2

Using PowerShell, if I run this command on my local computer, the output is "Running"

(Get-Service -Name Spooler).Status

If I run the same command inside an Invoke-Command script block, to get the status of the service on a remote computer

Invoke-Command -ComputerName 10.131.173.71 -Credential $cred -ScriptBlock {(Get-Service -Name Spooler).Status}

the output is

PSComputerName RunspaceId                           Value  
-------------- ----------                           -----  
10.131.173.71  5f1b9d02-4c60-47a9-b783-01fa89eb1d58 Running

How can I get the same output with the second command? (Running)

1
  • 2
    Wrap the whole thing in parentheses again and use .Value? Commented Jan 4, 2023 at 12:59

1 Answer 1

3

Here is one way to do it. You don't get the same value when remoting because the Status Property is an enum and it gets de-serialized that way. See this answer for more details.

[ServiceProcess.ServiceControllerStatus] (
    Invoke-Command -ComputerName 10.131.173.71 -Credential $cred -ScriptBlock {
        [int] (Get-Service Spooler).Status
    }
)
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.