PowerCLI

 View Only
  • 1.  Passing parameters to Invoke-VMScript (variables)

    Posted Oct 29, 2014 02:47 PM

    I am trying to use Invoke-VMScript to set new IP's on a list of servers.  The only access that I have to these servers is through vCenter console, so I thought this would be easier, faster, and less prone to errors. The problem is that I have to determine which nic to change the IP's on and I need to pass the new IP's into the Invoke-VMScript because they are going to be different for every server on the list.

    Invoke-VMScript -vm $vmname -guestUser 'dom\me\ -guestpassword '@ttt1234' -scripttype powershell -scripttext {

         $adapters = Get-wmiObjet win32_networkadapterconfiguration

         foreach ( $adapter in $adapters ) {

              if ($adpaters.ipaddress -match "$cIpaddress" ) {

                   $adapter.EnableStatic("$nIPaddress", "$nSubnet")

                   $adapter.SetGateways("$nGateway",1)

                   $adapter.SetDNSSErversearchorder($aDNS)

              }#end if

    }#end fe

    }#end scriptblock

    obviously all of these variable are populated earlier in the script, but they are not making it into the VM.

    I seem to need a way to populate the variable into a text, then put the text into the -scripttext.

    I thougth of writing it to a temp file  and then using get-content to bring it all back as a string and placing it in -scripttext.

    Any good ideas?



  • 2.  RE: Passing parameters to Invoke-VMScript (variables)

    Posted Oct 29, 2014 06:16 PM

    The ScriptText parameter on the Invoke-VMScript doesn't expect a script block but a string (or the path to a file).

    So you have to make sure that the variables in your script you want to pass on, are not interpreted on the machine where you launch the Invoke-VMScript cmdlet.

    One trick you can use is the here-string, something like this.

    $script = @'

    $adapters = Get-wmiObjet win32_networkadapterconfiguration

    foreach ( $adapter in $adapters ) {

        if ($adpaters.ipaddress -match "$cIpaddress" ) {

            $adapter.EnableStatic("$nIPaddress", "$nSubnet")

            $adapter.SetGateways("$nGateway",1)

            $adapter.SetDNSSErversearchorder($aDNS)

        }#end if

    }#end fe

    '@

    Invoke-VMScript -vm $vmname -guestUser 'dom\me\' -guestpassword '@ttt1234' -scripttype powershell -scripttext $script



  • 3.  RE: Passing parameters to Invoke-VMScript (variables)

    Posted Oct 29, 2014 07:31 PM

    That is the issue, I need every variable with a $nXXX to have the new information that I am adding, the new IP,SUBNET,GW and such. 

    Is there a way to populate the variables, convert it to a string and then put the string into the -scripttext?

    Something like, output to a file and then get the data from the file ( variables populated) and put that string into the -scripttext?



  • 4.  RE: Passing parameters to Invoke-VMScript (variables)

    Posted Oct 29, 2014 10:07 PM

    I see, then you could do something like this

    $script = @'

    $adapters = Get-wmiObjet win32_networkadapterconfiguration

    foreach ( $adapter in $adapters ) {

        if ($adpaters.ipaddress -match "#cIpaddress#" ) {

            $adapter.EnableStatic("#nIPaddress#", "#nSubnet#")

            $adapter.SetGateways("#nGateway#",1)

            $adapter.SetDNSSErversearchorder(#aDNS#)

        }#end if

    }#end fe

    '@

    # Get the correct value in the variables, then replace the marker in the string with that value

    $script = $script.Replace('#cIpaddress#',$cIpaddress).Replace('#nIPaddress#',$nIPaddress).Replace('#nSubnet#',$nSubnet).Replace('#nGateway',$nGateway).Replace('#aDNS#',$aDNS)

    Invoke-VMScript -vm $vmname -guestUser 'dom\me\' -guestpassword '@ttt1234' -scripttype powershell -scripttext $script



  • 5.  RE: Passing parameters to Invoke-VMScript (variables)

    Posted Sep 14, 2021 06:19 PM

    Slightly different issue, but was able to apply your logic.  Much Appreciated!

    Not the complete script, but wanted to share the main piece that was tripping me up.

    #Scripts
    $script1 = "netsh interface ipv4 show subinterfaces"
    $script2 = @'
    $Name = (Get-NetIPAddress -IPAddress 10.* | Get-NetAdapter | ? status -eq 'up' | Select-Object Name -ExpandProperty Name)
    netsh interface ipv4 set subinterface $Name mtu=1280 store=persistent
    '@

    foreach ($server in $servers)
    {
    $invoke1 = Invoke-VMScript -ScriptType Powershell -ScriptText $script1 -VM $server -GuestCredential $guestCredential
    $invoke2 = Invoke-VMScript -ScriptType Powershell -ScriptText $script2 -VM $server -GuestCredential $guestCredential
    }

    thank you!



  • 6.  RE: Passing parameters to Invoke-VMScript (variables)

    Posted Nov 28, 2022 01:09 PM

    Hello,

    I was searching for method to handle "Blue Print" or "Extensibility ABX Powershell" values and convert them to usable variables in guest for "Invoke-VMWscript" command.

    Another way than LucD advise is to print out the value into a file inside the guest and use it after with "Invoke-VMscript".

     

    This is how I did (in ABX):

    $tempvar = echo "echo $myvar | Out-File C:\temp\myfile.txt"

    (--> Just write the echo command string of your variable into variable)

    $script = $tempvar

    --> In $tempvar the $myvar variable is replaced by its value.  The result is that the ScriptText passed to Invoke-VMscript looks like "echo value | Out-File C:\temp\myfile.txt".

    Invoke-VMScript -ScriptText $script -VM $vmname -GuestPassword $password -GuestUser $user

    --> This command echo the value of the variable into a text file inside the guest.

     

    After that, the interesting value is written into C:\temp\myfile.txt.

    This can be used in another script with "Get-Content C:\temp\myfile.txt" to get the value back into a comprehensive guest variable or value.

    That's it!

    I hope this help.