Search code examples
powershellpowershell-4.0powershell-remoting

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 ?


Solution

  • 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.