Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple commands within ScriptBlock

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.

Invoke-Command -ComputerName cas-bkupexec -ScriptBlock { Get-Service;  Get-Process }

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

like image 611
Alexander Edwards Avatar asked Aug 01 '16 18:08

Alexander Edwards


People also ask

How do I run multiple commands in PowerShell?

To execute multiple commands in Windows PowerShell (a scripting language of Microsoft Windows), simply use a semicolon.

How do I run the same Command multiple times in PowerShell?

How to run a command multiple times in Windows PowerShell. Wrap your statement inside the curly braces of for ($i=1; $i -le n; $i++) { someCommand} , where n is a positive number and someCommand is any command. To access the variable (I use i but you can name it differently) you need to wrap it like this: $i .

Does PowerShell execute sequentially?

The Sequence keyword runs selected workflow activities sequentially. Workflow activities run in the order that they appear and do not run concurrently. The Sequence keyword is only valid in a PowerShell Workflow. The Sequence keyword is used in a Parallel script block to run selected commands sequentially.

What does Scriptblock do in PowerShell?

In the PowerShell programming language, a script block is a collection of statements or expressions that can be used as a single unit. A script block can accept arguments and return values. A script block returns the output of all the commands in the script block, either as a single object or as an array.


1 Answers

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 like breaks.

These options work:

Invoke-Command -ComputerName computer -ScriptBlock {
    (Get-Service)
    Get-Process
}

Or this curiously:

Invoke-Command -ComputerName computer -ScriptBlock {
    & { 
        Get-Service
        Get-Process
    }
}

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:

v2 -> v5: no issue
v2 -> itself: no issue
v2 -> other v2: no issue
v3 -> v2: no issue
v3 -> v5: no issue
v3 -> itself: no issue
v4 -> v2: no issue
v4 -> v3: no issue
v4 -> itself: no issue
v4 -> v5: no issue

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

like image 164
briantist Avatar answered Oct 25 '22 18:10

briantist