简体   繁体   中英

Difference between - command and ScriptBlock Powershell

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 ?

Difference between -command and -scriptblock

There is no difference in execution. -command is 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM