Variables del sistema y comando de invocación de Powershell

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 ".\ " $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?

preguntado el 29 de julio de 12 a las 03:07

2 Respuestas

Changes made using the SET comando son NO permanent, they apply to the current cmd process only and remain until the cmd el proceso finaliza.

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 & Me gusta esto:

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

Respondido 29 Jul 12, 08:07

Maybe I'm missing something, but shouldn't the following echo a value of 1 then: Invoke-Command -session $session {cmd /k "set myvar=1" `& "echo %myvar%"} It produces no output. - justin holbrook

Variable expansion occurs when the command is read (before it is set in your command). To delay the expansion of the variable add the /v switch when calling cmd.exe and use ! instead of %: Invoke-Command -session $session {cmd /v /k "set mytar=2 & echo !mytar!"} - Jon Z

Thank you for the examples and clear explanation. I was able to accomplish what I was working on. - justin holbrook

Yes, you need to string them together. The environment variables are being set in the first cmd session which exits, then a second one is started without them.

Respondido 29 Jul 12, 03:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.