powershellinvoke-commandsystem-variable

Powershell invoke-command and system variables


I'm working on automating patches for a vendor app. I wanted to use powershell to iterate over a list of servers and use remoting to install on each box. However, one aspect is giving me a big headache. I have to call a cmd script provided by the vendor to set system variables prior to executing the installation script. So I call the script like so in powershell:

Invoke-Command -session $session {cmd /k ($args[0]+"\data\env.bat")} -Args $lesDestDir

I see it execute as it echoes its work in my powershell window, but subsequent calls to the installation script ie:

Invoke-Command -session $session {perl ".\rollout-2010.pl " $args[0] " NC"} -Args $rollout

immediately exit saying the various system variables it uses have not been set. Can someone explain to me the scope of the system variables? Do they cease to exist after the invoke-command completes? Do I need to string together the two script calls in a single invoke-command so that the second script can access the system variables or something like that?


Solution

  • Changes made using the SET command are NOT permanent, they apply to the current cmd process only and remain until the cmd process is terminated.

    If possible set the environment variable using powershell syntax:

    Invoke-Command -session $session {$env:myvar = 1}
    

    Alternatively you could string the commands using the cmd command separator & like this:

    Invoke-Command -session $session {cmd /k "set myvar = 1" `& "set"}