Setting an Environment Variable via an EM script

Version 1

    Verified Product Versions

    Environment Manager 8.0Environment Manager 7.2


    If you use a custom script in EM to set up an an environment variable (such as the one below) you may notice unexpected results, such as the environment variable not being displayed via a SET command, and child processes of EM not recognising the variable.
    Set wshShell = CreateObject( "WScript.Shell" )
    Set wshSystemEnv = wshShell.Environment( "USER" )
    wshSystemEnv( "TestVar" ) = "Value"
    WScript.Echo wshSystemEnv( "TestVar" )



    1. The system does not refresh environment variables.

    2. A process inherits its environment block at init, so if the command window was already open prior to running the script it won't see the new variable.

    3. Creating an environment variable in a custom script does not inject the new variable back into the EM process block and therefore it doesn’t get passed on to child actions/processes.

    The example script will work to an extent.

    Set doesn’t show the variable but it will be visible via the Environment Variables section in Computer > Properties.

    Another solution is to use setx. This perhaps isn't as elegant as pure script as you have to shell out but it does refresh the variables and therefore lets processes outside the script see it.

    Dim WshShell
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run "setx TestVar Value", False
    If you run the script and then launch cmd and run Set you should see the new variable.

    However, because the scripted variable won't be injected into EM's process block the best solution is to use the Set Environment Variable action in EM.