Hi,
I am using the Invoke-Command with a Session to execute some installers on a remote computer. This works fine but for some reason setx.exe (to set Environment Variables) will not work as expected. When I create a new computer Environment Variable I am using the following command which works great:
$process = Invoke-Command -Session $Session -ScriptBlock { $result = Start-Process -FilePath "setx.exe" -ArgumentList 'MY_PATH "C:\Program Files\MyApplication" /M' -Wait -PassThru -Verb RunAs; }
However, when I try to edit a current Environment Variable like PATH it does not work. I use the following code:
$process = Invoke-Command -Session $Session -ScriptBlock { $result = Start-Process -FilePath "setx.exe" -ArgumentList 'PATH "%PATH%;%MY_PATH%\bin" /M' -Wait -PassThru -Verb RunAs; }
When I login on the remote computer and use "echo %PATH%" from the command prompt it prints the following:
%PATH%;%MY_PATH%
While I was expecting:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\MyApplication\bin
Any idea why I cannot append with this way to an already existing Environment Variable? The commands work locally when I execute it from the command prompt.