2

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
2
  • Take a look at Invoke-Command documentation, the examples should help Commented Sep 9, 2019 at 22:53
  • As an aside: A script-block literal has the form { ... }. ${...} is only ever used to reference variables (unambiguously), where the ... is the variable's name. Commented Sep 9, 2019 at 23:45

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

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.