Astora
Astora

Reputation: 725

Powershell - Invoke-Command with multiple functions and ArgumentList

How can I create something like this in powershell in the right way?

Invoke-Command -ComputerName $i -ScriptBlock ${function1 , function2} -credential $cred -ArgumentList parameter_function1,parameter_function1,parameter_function_2

Upvotes: 2

Views: 673

Answers (1)

mklement0
mklement0

Reputation: 437743

In the simplest case, rely on the automatic $args variable, which contains the array of (undeclared) arguments:

Invoke-Command -ComputerName $i -ScriptBlock {
  function1 $args[0]
  function2 $args[1]
} -credential $cred -ArgumentList arg_function1, arg_function2

As in any script block ({ ... }) you can explicitly declare parameters (and optionally declare their type, and add attributes - see about_functions_advanced_parameters), using param(...):

Invoke-Command -ComputerName $i -ScriptBlock {
  param($foo, $bar)
  function1 $foo
  function2 $bar
} -credential $cred -ArgumentList arg_function1, arg_function2

Upvotes: 2

Related Questions