invoke-command is typically used to execute a script block (-scriptBlock …) or a PowerShell script file (-filePath …). invoke-expression to execute a command (including arguments) whose text is stored in a variable. invoke-command cmdLet to execute it: $sb = {
write-output 'Hello from a script block'
}
write-output 'Going to invoke command with script block:'
invoke-command $sb
-argumentList parameter of invoke-command allows to pass parameters to a script block. Note, that the values of the parameters need to be separated by commas in the invocation: $sb = {
param($param_1, $param_2)
write-output "param_1 = $param_1"
write-output "param_2 = $param_2"
}
invoke-command -scriptBlock $sb -argumentList 42, 'Hello World'
-computerName parameter, it is possible to run a command on a remote computer. -filePath command option. # Create three sessions to the servers # named alpha, beta and gamma: # $ses = new-psSession -computerName alpha, beta, gamma # Run a command (or more accurately: a script block) all three servers: # invoke-command -session $ses -scriptBlock { … }
-credential.