1

ps1 is on remote machine . I am running below command from some other mahcine.

Is there any difference between using both below script ---

invoke-command -computer $MachineName -command { C:\hello.ps1 }
invoke-command -computer $MachineName -scriptblock{ C:\hello.ps1 }

Also, I am going to use for loop for multiple machine having same script name but having diff sequence of work that is located on each remote machine only . Want to understand the execution for second machine will go only if first get completed . Correct ?

2 Answers 2

4

Difference between -command and -scriptblock

There is no difference in execution. -commandis merely an alias for scriptblock. You can verify this by getting the parameter info for the invoke-command command

(Get-Command -Name Invoke-Command).Parameters.Values | select name, aliases

Sequential execution

Yes, the execution is sequential. The command you specify will execute on the second machine after the command has completed on the first machine.

According to the help

These commands run synchronously (one at a time). When the commands complete, the output of the commands from all of the computers is saved in the $version variable. The output includes the name of the computer from which the data originated.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks ! Any help for my second query as I want my first invoke to execute completely then want to go for other server in loop... Is is taken care by for loop and invoke automatically ?
That actually depends on how Invoke-Command is implemented when multiple computernames get passed. I would assume this is sequential but I'd have to verify.
Actually I cannot test on live machine now as some work is going on for some more days ..
$MACHINES = Get-Content "Somelocation" foreach ( $Machine in $MACHINES) { invoke-command -computer $Machine -command { C:\hello.ps1 } } hello.ps1 is available on each remote machine with different code inside and I want to execute for each server sequentially from one fixed server.
Thank You So much . I will do a testing once i got a chance on live system .
0

To answer your second question: Invoke-Command works in parallel. It runs what ever is mentioned in the script block to all the machines in parallel. By default, powershell will talk upto 32 computers at once. If you specify more than that, it will queue them up so that as one computer completes, the next one in line will begin. However, I believe, you can increase that number by specifying the -throttlelimit parameter of the Invoke-Command.

8 Comments

I am using for loop , Still it will do in parallel ? Or 1 Machine at a time .. Below is the code. MACHINES = Get-Content "Somelocation" foreach ( $Machine in $MACHINES) { invoke-command -computer $Machine -command { C:\hello.ps1 } }
Lieven Keersmaekers suggested that it will be sequential one after one ..
I tried on two live remote machines and its sequential not parallel.
The ThrottleLimit only applies when running invoke-command as a job, not as OP currently is doing.
"Invoke-Command" does the parallel processing. Since you started your command with a "Get-Content", that might not help you with parallel processing. Don Jones, has explained it better - [link]blogs.technet.microsoft.com/heyscriptingguy/2011/06/13/…
|

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.