当尝试使用[datetime] :: ParseExact函数转换字符串时遇到一个问题。 我正在使用Get-Filemetadata函数(请参见https://gallery.technet.microsoft.com/scriptcenter/Get-FileMetaData-3a7ddea7)从文件中提取记录,创建,修改等的日期时间。 尝试将记录日期转换为日期时间时出错(创建和修改日期就可以了吗?!)。 我正在使用Powershell ISE。
为缩小原因,我将记录日期的输出复制到了一个单独的脚本中,所以现在我有两个字符串和一个用于解析的模式。 仅在使用此复制的记录日期时才会出现此问题。如果我在代码中手动输入日期,则转换工作正常(按预期)。 它们在外观上是相同的,但是在记录日期,我得到了错误:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
与我的主要脚本中的错误相同。
我将脚本保存到文件中,并使用记事本++将其打开。看起来就像在Powershell ISE中一样。我使用记事本++保存了一个副本,然后使用Powershell ISE重新打开了该副本。
但是现在第二个字符串显示为$raw = "12.‎08.‎2018 â€â€Ž13:24"
,所以我想这就是问题所在。
我已经尝试过拆分并重新合并记录日期以匹配模式,但是错误仍然存在。因此,我假设那些隐藏的字符直接来自文件元数据,并且在拆分/合并期间不会被删除。.
Powershell ISE:
$pattern = 'dd.MM.yyyy HH:mm'
$raw = "28.08.2018 13:24" #manually entered
[DateTime]::ParseExact($raw, $pattern, $null) #output: Dienstag, 28. August 2018 13:24:00
$raw = "12.08.2018 13:24" #copied from powershell output
[DateTime]::ParseExact($raw, $pattern, $null) #output: Exception error..
使用Powershell ISE重新打开从Notepad ++复制的内容:
$pattern = 'dd.MM.yyyy HH:mm'
$raw = "28.08.2018 13:24"
[DateTime]::ParseExact($raw, $pattern, $null)
$raw = "12.‎08.‎2018 â€â€Ž13:24"
[DateTime]::ParseExact($raw, $pattern, $null)
我需要将get-filemetadata函数的记录日期输出解析为datetime以便进行排序。
先谢谢了。