Getting get-service results from remote system into script variables

DevSupport used Ask the Experts™
I am trying to get the results from a command Get-Service remotely and get the "status" result into a text box.

When I do Get-Service -DisplayName "windows audio" locally

I get

Status   Name               DisplayName                          
------   ----               -----------                          
Running  AudioSrv           Windows Audio

I would like to run it remotely using invoke-command and obtain the "Running" into an array or something, so that I can show in text box.

When I try the below I get $AppState as Name or "Audiosrv", but I am unable to get the Status value into array.

$AppState = Invoke-Command -Computername $objappsvrname.Text -ArgumentList $dispname -ScriptBlock {Param($DisplayName); Get-Service -DisplayName $DisplayName}

Please let me know how I can get the  status "Running" into $AppState

Please let me know if you need more info.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Your command will return an object with the usual properties, and you can access them the same way you would with a local execution:
Or you can do it on the remote side if you only want the Status:
$AppState = Invoke-Command -Computername $objappsvrname.Text -ArgumentList $dispname -ScriptBlock {Param($DisplayName); (Get-Service -DisplayName $DisplayName).Status}

Open in new window

Or like this:
$AppState = Invoke-Command -Computername $objappsvrname.Text -ArgumentList $dispname -ScriptBlock {Param($DisplayName); Get-Service -DisplayName $DisplayName | Select-Object -ExpandPoperty Status}

Open in new window


Thank You oBDA!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial