Get-service output not working as it shoud?

Foreach ($service in $services){
get-service -computername CLPROD03|where-object {$_.DisplayName -EQ $service}|select-object -Property 'status'
  If ($_.status -eq 'Running')
      {
      Write-output `$_.status`
       }
 }

Status

Running
Running
Running
Stopped

For the above code the If ($_.status -eq ‘Running’)
Should only go into the if statement when “Running” and thus only print all the “Running”
but it is printing “Stopped” which to me the “IF” statement in not working as it should.

Any suggestion would be greatly appreciated.

Thanks
Sunny

The main problem is you are referencing the $_ token. You are not piping to it and the for loop has a token called ‘$service’. It’s easier to leverage the cmdlet parameters versus doing a where clause. Make sure you read the documentation and examples. Take a look at the following:

PS C:\Users\rasim>
$services = 'Application Identity', 'Application Information'
Foreach ($service in $services){
    Get-Service -Name $service 
}

Status   Name               DisplayName
------   ----               -----------
Stopped  AppIDSvc           Application Identity
Running  Appinfo            Application Information

PS C:\Users\rasim> 
$services = 'Application Identity', 'Application Information'
Foreach ($service in $services){
    Get-Service -Name $service |
        Where-Object { $_.Status -eq 'Running' }
}

Status   Name               DisplayName
------   ----               -----------
Running  Appinfo            Application Information
1 Like

Hi Rob

Understood but from a “TESTING” perspective how can i test to see what the status is. I want to be 100% sure that I want the following:

“get-service -computername CLPROD03|where-object {$.DisplayName -EQ $service}|Where-Object { $.Status -eq ‘Running’ }”

Is only filtering “Running”.

Thus how can i see from a Write-output or any other means to see the output of the above get-service execution.

Also i added the tick in front of the code. May still need to understand how to do that.

Thanks
Sunny

Actually it is even much easier. Both parameters - -Name and -ComputerName will accept multiple values. So - no loop needed - you can provide a list of services and a list of computers you want to query just like this:

$computerList = 'CLPROD03',$ENV:ComputerName
$serviceList = 'WSearch','wuauserv','WinRM'
Get-Service -Name $serviceList -ComputerName $computerList | 
    Where-Object -Property Status -EQ -Value 'Running' |
        Sort-Object -Property MachineName, DisplayName |
            Select-Object -Property MachineName, Status, DisplayName

This will output a list of running services from computers you specified.

1 Like

Your code is effectively this

Foreach ($service in $services){
get-service -computername CLPROD03|where-object {$_.DisplayName -EQ $service}|select-object -Property 'status'
 }

That if wasn’t doing anything really because in your loop $_ is nonexistent. You specifically did a foreach($service in $services) loop which means the variable populated in each iteration is $service. If you want to use $$_ then you should instead use a Foreach-Object. Look at these examples for clarification of the different loops.

$services | Foreach-Object{
    # Can also use $psitem instead of $_
     Write-Host Current service: $_
 }
foreach($service in $services){
     Write-Host Current service: $service
 }

As Olaf stated a loop is not needed. If you really just want to see running for every service that’s running…

$computerList = 'CLPROD03',$ENV:ComputerName
$serviceList = 'WSearch','wuauserv','WinRM'
Get-Service -Name $serviceList -ComputerName $computerList |
    Where-Object status -eq 'running' | Foreach-Object status

Thank you [krzydoug]

As always Thank you [Olaf]