0

Context: This script/function lives on my local machine. I want to pass variables to the function, then pass them into an invoke-command script block to be run on a remote machine.

This code works, but I don't feel like its good practice, or the best way of doing it, as I have to pass my params around multiple times, and it all just feels a bit clunky.

Do I need to supply $params 4 times here to achieve my outcome?

function Create-NewVM
{
  param($param1, $param2, $param3, $param4)

  $ScriptBlock = 
  {
    param ($param1, $param2, $param3, $param4)
    # do a bunch of stuff on my remote machine
  }

  $s = New-PSSession hypervhost.fqdn
  Invoke-Command -Session $s -ScriptBlock $ScriptBlock -ArgumentList $param1, $param2, $param3, $param4
}

Create-NewVM -ComputerName "vm01" $param1 $param2 $param3 $param4
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
ajhstn
  • 21
  • 4
  • There's nothing wrong with the way you use `$var` in the multiline string, except that there must be no whitespace before `"@`. The rest of the code you posted is too incomplete to even guess at what the problem might be. – Ansgar Wiechers Jul 31 '16 at 21:46
  • Hello thanks for that Ansgar. If you were to remove $htmlBlock etc, just looking at the structure of the code, can you give any assistance there? My main question is do i need to submit my $params 4 times to achieve my outcome? – ajhstn Jul 31 '16 at 22:40
  • @AnsgarWiechers i have simplified the code, hopefully it makes more sense now? – ajhstn Jul 31 '16 at 22:54
  • If your question is whether you should pass parameters you want to use inside the scriptblock the way you do: yes, you should. You may want to add some parameter validation on top of that. – Ansgar Wiechers Jul 31 '16 at 23:06

0 Answers0