[英]PowerShell 7; String to DateTime?
This piece of code works fine in PowerShell 5. It returns the following date/time, depending on which OS and OS language is used;这段代码在 PowerShell 中运行良好 5. 它返回以下日期/时间,具体取决于使用的操作系统和操作系统语言; "Wed, 04 Mar 2015 07:39:54 GMT" “星期三,2015 年 3 月 4 日 07:39:54 GMT”
"woensdag 4 maart 2015 08:39:54" “woensdag 4 maart 2015 08:39:54”
However, in PowerShell 7 it no longer works and gives me the following error.但是,在 PowerShell 7 中,它不再有效,并出现以下错误。 I've spent hours online to see what happens, but.. Whatever I try, it never works.我花了几个小时在线查看会发生什么,但是.. 无论我尝试什么,它都行不通。 How can I transform a String in a DateTime object in PowerShell 7.2.4?如何在 PowerShell 7.2.4 中转换 DateTime object 中的字符串?
$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".
In PowerShell 7.x the type of the header fields has changed from String
to String[]
, that is an array of strings.在 PowerShell 7.x 中, header 字段的类型已从String
更改为String[]
,即字符串数组。
So just take the first element of the array to let the conversion succeed:所以只要取数组的第一个元素就可以让转换成功:
$LastModifiedDateTime = [DateTime] $result.Headers['Last-Modified'][0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.