locked
Need help writing better PowerShell code. RRS feed

  • Question

  • Hi, please help me write the below function better.

    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

    • Edited by ahdlstn Sunday, July 31, 2016 10:51 PM
    Sunday, July 31, 2016 9:15 PM

Answers

  • Whatever is returned from Invoke-Command will be returned by the function.  From the posted code there is no point in using a function:

    The following is identical.  Don't wrap things in functions just because it looks cool.  It makes debugging harder and introduces all manner of issues.

    $ScriptBlock = {
    	param ($param1,
    		$param2,
    		$param3,
    		$param4)
    	# do a bunch of stuff on my remote machine
    }
    
    Invoke-Command -ComputerName vm01 -ScriptBlock $ScriptBlock -ArgumentList $param1, $param2, $param3, $param4
    	


    \_(ツ)_/

    • Marked as answer by ahdlstn Sunday, July 31, 2016 11:46 PM
    Sunday, July 31, 2016 11:00 PM

All replies

  • Sorry but tat is how it works.

    Your issue appears to be a lack of basic understanding of programming.  All programmed systems work pretty much the same way.

    You are not returning anything from the script block or from the function so there is no way to critique what you are asking.


    \_(ツ)_/

    Sunday, July 31, 2016 10:47 PM
  • Hi jrv, i have simplified my original post, hopefully it makes more sense now?
    Sunday, July 31, 2016 10:52 PM
  • Whatever is returned from Invoke-Command will be returned by the function.  From the posted code there is no point in using a function:

    The following is identical.  Don't wrap things in functions just because it looks cool.  It makes debugging harder and introduces all manner of issues.

    $ScriptBlock = {
    	param ($param1,
    		$param2,
    		$param3,
    		$param4)
    	# do a bunch of stuff on my remote machine
    }
    
    Invoke-Command -ComputerName vm01 -ScriptBlock $ScriptBlock -ArgumentList $param1, $param2, $param3, $param4
    	


    \_(ツ)_/

    • Marked as answer by ahdlstn Sunday, July 31, 2016 11:46 PM
    Sunday, July 31, 2016 11:00 PM