我以Media Created
这种方式从文件资源管理器中检索列字段:
$mCreated = $shellfolder.GetDetailsOf($shellfile, 208);
这很好用。它给了我字符串“5/7/2017 4:09 PM”
问题:
当我尝试将字符串转换为格式化日期时,出现此错误:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Client\testVideos\anotherTest.ps1:28 char:9
+ $formattedDate = [Datetime]::ParseExact("$mCreated".Trim(), ' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
我试过的:
分别尝试了这些,都给了我同样的错误:
$formattedDate = [Datetime]::ParseExact("$mCreated".Trim(), 'yyyyMMddTHHmmss', $null)
$formattedDate = [Datetime]::ParseExact("$mCreated".Trim(), 'yyyyMMddTHHmmss', [Globalization.CultureInfo]::InvariantCulture)
# This line actually gives me a different error
$formattedDate = Get-Date "$mCreated".Trim() -Format "yyyyMMddTHHmmss"
# ERROR: Get-Date : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input
什么有效:
我也一直Created Date
在使用相同的代码并以相同的方式对其进行格式化,并且它有效!
创建日期为“2/12/2021 1:10 PM”、“2/4/2021 3:39 PM”、“2/2/2021 9:00 AM”或“2/4/2021 3:54”下午”
# This formats it successfully with no error
$createdDate = $shellfolder.GetDetailsOf($shellfile, 4);
$formattedDate = $createdDate | Get-Date -Format "yyyyMMddTHHmmss"
相关但最终无济于事:
https://stackoverflow.com/questions/42784919/parseexact-string-was-not-recognized-as-a-valid-datetime https://stackoverflow.com/questions/48637312/convert-string-to-powershell-datetime