不支持将数组元素传送到 New-TimeSpan 返回方法
Piping array elements into New-TimeSpan returning method not supported
我有一个脚本可以检查某些文件的最后写入时间,如果它们超出了我希望它们位于的时间参数,它们会向我发送一条文本。这是我以前的(工作)方式:
$lastupdate = (Get-ChildItem N:\BYUI-played.xml).LastWriteTime
$currentdate = get-date
$difference =NEW-TIMESPAN –Start $lastupdate –End $currentdate
IF ($difference -gt "0.0:31:0.0") {
$Sesherror = 1
Log-Write "$currentdate [ERROR] BYUI-JustPlayed is down"
}
六次...然后程序底部的 if 将检查 $Sesherror
的值,然后...那么您可能会算出其余部分。显然很麻烦而且写得不是很好。所以,我决定重构它,我想出了我认为圆滑的东西:
$lastupdate = @((Get-ChildItem N:\BYUI-played.xml).LastWriteTime,(Get-ChildItem N:\byui-now.xml).LastWriteTime,(Get-ChildItem N:\KBYR-played.xml).LastWriteTime,(Get-ChildItem N:\KBYR-now.xml).LastWriteTime,(Get-ChildItem N:\KBYI-played.xml).LastWriteTime,(Get-ChildItem N:\KBYI-now.xml).LastWriteTime)
Try {
$lastupdate | % {
if (NEW-TIMESPAN –Start $lastupdate –End $currentdate -gt "0.1:0:0.0") {
$Sesherror = 1
Log-Write "$currentdate [ERROR] $lastupdate is down"
}}
}
Catch {
Write-Warning "Something went wrong with the algorithim"
Write-Host "$ERROR" -foregroundcolor Red
Write-Warning "Terminating error. Shutting down."
cmd /c pause
exit
}
再次,轮询底部的 $Sesherror
变量。但是,当它运行时,我收到此错误消息的六个实例:
Cannot convert 'System.Object[]' to the type 'System.DateTime' required by parameter 'Start'. Specified method is not supported.
所以我不得不假设它与作为 "Time" 类型通过管道传入的数组有关。但是,我已经回显了数组,它们都输出了正确的时间类型。所以我想这个问题有两个方面:
1.为什么foreach循环不接受数组?
2. 如果它永远不会,那么完成我想要完成的事情的最佳方法是什么?
您的直接问题是您在 ForEach-Object
(%
) 脚本块中使用数组值 $lastupdate
变量而不是自动迭代变量 $_
.
因此您将 array 传递给 New-TimeSpan
的 -Start
参数,但失败了。
此外,可以对您的脚本进行多项优化:
$files = 'N:\BYUI-played.xml', 'N:\byui-now.xml', 'N:\KBYR-played.xml', 'N:\KBYR-now.xml', 'N:\KBYI-played.xml', 'N:\KBYI-now.xml'
$lastupdate = @(Get-Item $files | % { $_.LastWriteTime })
$currentdate = get-date
Try {
$lastupdate | % {
if (($_ – $currentdate) -gt "0.1:0:0.0") {
$Sesherror = 1
Log-Write "$currentdate [ERROR] $lastupdate is down"
}
}
} Catch {
Write-Warning "Something went wrong with the algorithm"
Write-Host "$ERROR" -foregroundcolor Red
Write-Warning "Terminating error. Shutting down."
cmd /c pause
exit
}
Get-ChildItem
/ Get-Item
接受一个 数组 路径,因此不需要为每个文件单独调用。
从另一个隐式地减去两个 [datetime]
实例 returns 一个 [timespan]
实例 - 不需要 New-TimeSpan
.
此外,您可以在循环外基于"0.1:0:0.0"
构造一个[timespan]
实例,并将其存储在变量中以供-gt
比较使用,所以为了避免在每次循环迭代中都必须将字符串值转换为 [timespan]
实例,尽管这种优化对现实世界的影响可以忽略不计。
我有一个脚本可以检查某些文件的最后写入时间,如果它们超出了我希望它们位于的时间参数,它们会向我发送一条文本。这是我以前的(工作)方式:
$lastupdate = (Get-ChildItem N:\BYUI-played.xml).LastWriteTime
$currentdate = get-date
$difference =NEW-TIMESPAN –Start $lastupdate –End $currentdate
IF ($difference -gt "0.0:31:0.0") {
$Sesherror = 1
Log-Write "$currentdate [ERROR] BYUI-JustPlayed is down"
}
六次...然后程序底部的 if 将检查 $Sesherror
的值,然后...那么您可能会算出其余部分。显然很麻烦而且写得不是很好。所以,我决定重构它,我想出了我认为圆滑的东西:
$lastupdate = @((Get-ChildItem N:\BYUI-played.xml).LastWriteTime,(Get-ChildItem N:\byui-now.xml).LastWriteTime,(Get-ChildItem N:\KBYR-played.xml).LastWriteTime,(Get-ChildItem N:\KBYR-now.xml).LastWriteTime,(Get-ChildItem N:\KBYI-played.xml).LastWriteTime,(Get-ChildItem N:\KBYI-now.xml).LastWriteTime)
Try {
$lastupdate | % {
if (NEW-TIMESPAN –Start $lastupdate –End $currentdate -gt "0.1:0:0.0") {
$Sesherror = 1
Log-Write "$currentdate [ERROR] $lastupdate is down"
}}
}
Catch {
Write-Warning "Something went wrong with the algorithim"
Write-Host "$ERROR" -foregroundcolor Red
Write-Warning "Terminating error. Shutting down."
cmd /c pause
exit
}
再次,轮询底部的 $Sesherror
变量。但是,当它运行时,我收到此错误消息的六个实例:
Cannot convert 'System.Object[]' to the type 'System.DateTime' required by parameter 'Start'. Specified method is not supported.
所以我不得不假设它与作为 "Time" 类型通过管道传入的数组有关。但是,我已经回显了数组,它们都输出了正确的时间类型。所以我想这个问题有两个方面:
1.为什么foreach循环不接受数组?
2. 如果它永远不会,那么完成我想要完成的事情的最佳方法是什么?
您的直接问题是您在 ForEach-Object
(%
) 脚本块中使用数组值 $lastupdate
变量而不是自动迭代变量 $_
.
因此您将 array 传递给 New-TimeSpan
的 -Start
参数,但失败了。
此外,可以对您的脚本进行多项优化:
$files = 'N:\BYUI-played.xml', 'N:\byui-now.xml', 'N:\KBYR-played.xml', 'N:\KBYR-now.xml', 'N:\KBYI-played.xml', 'N:\KBYI-now.xml'
$lastupdate = @(Get-Item $files | % { $_.LastWriteTime })
$currentdate = get-date
Try {
$lastupdate | % {
if (($_ – $currentdate) -gt "0.1:0:0.0") {
$Sesherror = 1
Log-Write "$currentdate [ERROR] $lastupdate is down"
}
}
} Catch {
Write-Warning "Something went wrong with the algorithm"
Write-Host "$ERROR" -foregroundcolor Red
Write-Warning "Terminating error. Shutting down."
cmd /c pause
exit
}
Get-ChildItem
/Get-Item
接受一个 数组 路径,因此不需要为每个文件单独调用。从另一个隐式地减去两个
[datetime]
实例 returns 一个[timespan]
实例 - 不需要New-TimeSpan
.此外,您可以在循环外基于
"0.1:0:0.0"
构造一个[timespan]
实例,并将其存储在变量中以供-gt
比较使用,所以为了避免在每次循环迭代中都必须将字符串值转换为[timespan]
实例,尽管这种优化对现实世界的影响可以忽略不计。