0

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.

1 Answer 1

1

The Where-Object propertyName -op value syntax was introduced in PowerShell 3.0.

Change to:

... |Where-Object {$_.Status -ne 'Running'} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.