这段代码在PowerShell 5中运行良好。它返回以下日期/时间,取决于所使用的操作系统和操作系统语言;"Wed,20153.04 07:39:54格林尼治时间“
伍恩斯达4 maart 2015 08:39:54
但是,在PowerShell 7中,它不再起作用,并给出了以下错误。我花了好几个小时在网上看看会发生什么,但是..。不管我怎么试,都没用。如何在DateTime对象中转换PowerShell 7.2.4中的字符串?
$result = Invoke-WebRequest -Method HEAD -Uri "https://microsoft.com" -UseBasicParsing -ErrorAction:Stop -TimeoutSec 30
$LastModifiedDateTime = [DateTime] $result.Headers['Last-Modified']
InvalidArgument: Cannot convert the "System.String[]" value of type "System.String[]" to type "System.DateTime".发布于 2022-06-19 17:41:55
在PowerShell 7.x中,标头字段的类型已从String更改为String[],即字符串数组。
因此,只需使用数组的第一个元素就可以让转换成功:
$LastModifiedDateTime = [DateTime] $result.Headers['Last-Modified'][0]https://stackoverflow.com/questions/72678967
复制相似问题