Multiple Commands in ScriptBlock

I am having trouble running multiple commands in the Scriptblock parameter. All documentation indicates the use of a semicolon to separate cmdlets. Using the same methodology for separating cmdlets with semicolons works on my local machine, but not on the remote machine using the invoke command.

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

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

How can I achieve the desired result of the successful work of both teams and get the result of each of them?

+4
source share
2 answers

. . , , .

:

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

:

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

( ) , , , , .

:

, v5 v2, , .

v5 , .

:

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

, , v5 v2.

+2

, , .

$Results = Invoke-Command -ComputerName cas-bkupexec -ScriptBlock { (Get-Service), (Get-Process) }

#Services
$Results[0]

#Processes
$Results[1]
+1

Source: https://habr.com/ru/post/1649817/


All Articles