Reputation: 1243
I have script which monitors if services from txt files are running or not
standard list.txt:
Amsp
LTService
Bellow block works on 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 variable:
Name Status
---- ----
Amsp Stopped
LTService Running
However, this block fails only on 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".
Modified it in this way:
$faultyservice = $serviceList | where-object {Status -ne "Running"}
Then getting:
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.
Upvotes: 0
Views: 264
Reputation: 174465
The Where-Object propertyName -op value
syntax was introduced in PowerShell 3.0.
Change to:
... |Where-Object {$_.Status -ne 'Running'}
Upvotes: 1