Powershell 2.0 - Where-Object:无法绑定参数 'FilterScript'
Powershell 2.0 - Where-Object : Cannot bind parameter 'FilterScript'
我有脚本可以监控来自 txt 文件的服务是否运行
标准list.txt:
Amsp
LTService
Bellow 块适用于 Powershell 2.0/3.0
$servicesToMonitor = Get-Content "$scriptpath\standard list.txt"
$servicebool = $true
$serviceList =@()
$status = 0
foreach($s in $servicesToMonitor){
$tempservice = Get-Service $s -ErrorAction SilentlyContinue | Select-Object -Property Name, Status
$serviceList += $tempservice
if($tempservice.Status -like "Running"){
#Write-Host $tempservice.Name "-" $tempservice.Status
}elseif($tempservice -ne $null){
$servicebool = $false
#Write-Host $tempservice.Name "-" $tempservice.Status
}
}
$serviceList 变量:
Name Status
---- ----
Amsp Stopped
LTService Running
但是,此块仅在 PowerShell 2.0 上失败
$faultyservice = $serviceList | where Status -ne "Running"
write-host "CRITICAL:" $faultyservice
$status = 2
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "Status" value of type "System.String" to type "System.Manageme
nt.Automation.ScriptBlock".
修改为:
$faultyservice = $serviceList | where-object {Status -ne "Running"}
然后得到:
The term 'Status' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name
, or if a path was included, verify that the path is correct and try again.
PowerShell 3.0 中引入了 Where-Object propertyName -op value
语法。
更改为:
... |Where-Object {$_.Status -ne 'Running'}
我有脚本可以监控来自 txt 文件的服务是否运行
标准list.txt:
Amsp
LTService
Bellow 块适用于 Powershell 2.0/3.0
$servicesToMonitor = Get-Content "$scriptpath\standard list.txt"
$servicebool = $true
$serviceList =@()
$status = 0
foreach($s in $servicesToMonitor){
$tempservice = Get-Service $s -ErrorAction SilentlyContinue | Select-Object -Property Name, Status
$serviceList += $tempservice
if($tempservice.Status -like "Running"){
#Write-Host $tempservice.Name "-" $tempservice.Status
}elseif($tempservice -ne $null){
$servicebool = $false
#Write-Host $tempservice.Name "-" $tempservice.Status
}
}
$serviceList 变量:
Name Status
---- ----
Amsp Stopped
LTService Running
但是,此块仅在 PowerShell 2.0 上失败
$faultyservice = $serviceList | where Status -ne "Running"
write-host "CRITICAL:" $faultyservice
$status = 2
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "Status" value of type "System.String" to type "System.Manageme
nt.Automation.ScriptBlock".
修改为:
$faultyservice = $serviceList | where-object {Status -ne "Running"}
然后得到:
The term 'Status' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name
, or if a path was included, verify that the path is correct and try again.
PowerShell 3.0 中引入了 Where-Object propertyName -op value
语法。
更改为:
... |Where-Object {$_.Status -ne 'Running'}