Home > Blockchain >  Can not refresh $env:variable inside of Start-Job block
Can not refresh $env:variable inside of Start-Job block

Time:10-29

I am trying to have a controller inside of mother script. It has to check status of $Env:var and depending of value do something. The problem is I can not refresh it anyhow. It catches the value on the beginning of execution and then $Env:var stay static, despite on what happens with $Env:var outside of Start-Job block.

Start-Job -Name Controller -scriptblock {     
    
            while($true){ 
    
                    if ($Env:var -eq 100){
    
                            # lot of different stuff
                    }
        
                    Start-Sleep -Seconds 5
            }
} 

Unfortunately i am limited with PS 3.0 on my environment.

CodePudding user response:

Indeed, the $Env Environment variables are set once the PowerShell process is launched, and they don't get refreshed when the value changes.

If the environment variables change outside of PowerShell, you have to read them directly from the registry to get the current value. The base locations are:

System Environment Variables

Get-Item "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

User Environment Variables

Get-Item HKCU:\Environment
  • Related