我正在使用Powershell通过REST API从Web应用程序提取数据,我需要将日期/时间转换为结果的另一种格式。时间采用ISO组合的日期时间格式(即2019-06-24T03:08:21),我希望它是“ yyyyMMdd”。数据以JSON格式接收
我尝试使用确切的日期而不是$ WebResponse.occurred变量,但仍然出错。
代码:
$BeginDate = (Get-Date).AddDays(-7).ToString("yyyyMMdd")
$EndDate = Get-Date -Format yyyyMMdd
$WebResponse = Invoke-RestMethod -H @{'Authorization' = 'Token **'} https://webhost.com/$BeginDate-$EndDate/
$WebResponse | foreach-object {[datetime]::parseexact(($WebResponse.occurred).Trim(), 'yyyy-MM-ddTHH:mm:ss',$null).ToString('yyyy-MM-dd')} | Export-CSV $File -NoTypeInformation
这是我得到的错误
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\bellc\Desktop\test.ps1:12 char:33
+ ... ach-object {[datetime]::parseexact(($WebResponse.occurred).Trim(), 'y ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException