Multiple commands within ScriptBlock

Question:

I’m having trouble running multiple commands within a Scriptblock parameter. All documentation points towards using a semicolon for separation between cmdlets. Using the same methodology of separating cmdlets via a semicolon works on my local machine, but not on a remote machine through invoke-command.

For example, the below code will only return the result of Get-Service, and not Get-Process.

How can I achieve the desired result of both commands successfully running and receive the output of each?

Answer:

I am able to reproduce this.. it only returns the first statement. It’s not the semicolon, it does the same thing if you use line breaks.

These options work:

Or this curiously:

Since wrapping one of them in ( ) works I feel like this must be some quirk of the pipeline but I haven’t been able to figure out exactly what it’s doing yet.

Update:

After reading your comment that your source machine was running v5 and the remote v2, I realized that my testing did the same.

When I remoted from the v5 into itself, the issue disappeared.

Remoting from:

Not every combination here, but it seems like it really may be a bug from v5 to v2.

Source:

Multiple commands within ScriptBlock by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply