I'm trying to output the status of a service running on a remote server. I'm doing this for multiple servers so the format I want will read:
ServerName1: Stopped
ServerName2: Running
ServerName3: Running
My code:
$servername1 = get-service -name "someService" -computername ServerName1 | Format-Wide -Property Status | Out-String
Write-Host -nonewline "ServerName: $servername1"
My result:
ServerName1:
Stopped
ServerName2:
Running
ServerName3:
Running
-----------------------------------------------
Another variation I've tried:
$servername1 = get-service -name "someService" -computername ServerName1 | Format-Table @{n='Status';e={$_.Status}} -HideTableHeaders
if ($servername1 = "Running") {
Write-Host -nonewline "ServerName1: Running"
} elseif ($servername1 = "Stopped") {
Write-Host -nonewline "ServerName1: Stopped"
}
And the results from that actually match the format I want, but the status reads "Running" no matter what. Thanks for your help!