Using Path in Local System Context

I’m using GFI MAX Remote Management to deploy some PowerShell scripts to servers. The scripts need access to a new directory I set up, C:\Scripts\PowerShell, so I used a PowerShell command to add that path to the Path environment variable in the machine scope.

$ScriptsPath = 'C:\Scripts\PowerShell\'
$CurrentMachinePath = [Environment]::GetEnvironmentVariable("Path", `
    [System.EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable( "Path", $CurrentMachinePath + ";" + `
    $ScriptsPath, [System.EnvironmentVariableTarget]::Machine )

I couldn’t figure out why my scripts weren’t running. When I checked the path from a command prompt, it looked fine:

Path issue 1

Finally it occurred to me that since the MAX RM agent runs in the Local System account, maybe the Path won’t be updated until the computer reboots. To check, I ran a small PowerShell script from MAX RM to see what the Local System account sees as the environment:

Get-ChildItem env: | Out-String -Width 4096 -Stream `
    | ForEach-Object { $_.TrimEnd() }

Checking the results in the MAX RM dashboard, I see that sure enough, the Path has not been updated, and it runs in the machine context (USERNAME = MachineName$):

Path issue 2

So, reboot and voila, the path has now been updated:

Path issue 3

…and scripts that depend on the path are running.

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.