Setting environment Variables

I have been tasked with adding or changing environment variables because of a change in licensing locations. They are located in the System Environment Variables area.
I need to check if a Variable Name is there and if not, create one and add the Variable Value. If the Variable Name is there then I need to check if it is the proper Variable Value and if not change it.

Sounds like a lot to put into the task but there are a few hundred machines on site and I thought a BF Task would work best.
Any ideas?

@mwdbrown, can you provide additional details? How would you implement this process via OS native scripting language?

Example on Windows:

wait cmd /c setx -m JAVA_HOME “C:\Java”

If it’s a per-machine environment variable, you should find it in the Registry at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

One complication is that some variables are defined as “REG_SZ”, others as “REG_EXPAND_SZ”. “REG_SZ” is much easier to deal with, as the format for adding/modifying a REG_EXPAND_SZ is in binary.

On a system that has the variable, try this to see its type and value:

q: (name of it, it as string, type of it) of values of keys "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" of registry
A: ComSpec, %25SystemRoot%25\system32\cmd.exe%00, REG_EXPAND_SZ
A: DriverData, C:\Windows\System32\Drivers\DriverData, REG_SZ
A: OS, Windows_NT, REG_SZ
A: Path, C:\Program Files\AdoptOpenJDK\jdk-8.0.265.01-hotspot\bin;%25SystemRoot%25\system32;%25SystemRoot%25;%25SystemRoot%25\System32\Wbem;%25SYSTEMROOT%25\System32\WindowsPowerShell\v1.0\;%25SYSTEMROOT%25\System32\OpenSSH\;C:\SysInternals\;C:\Program Files\kdiff3\bin;C:\Go\bin;C:\Program Files\OpenSSL-Win64\bin%00, REG_EXPAND_SZ
A: PATHEXT, .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC, REG_SZ
A: PROCESSOR_ARCHITECTURE, AMD64, REG_SZ
A: PSModulePath, %25ProgramFiles%25\WindowsPowerShell\Modules;%25SystemRoot%25\system32\WindowsPowerShell\v1.0\Modules%00, REG_EXPAND_SZ
A: TEMP, %25SystemRoot%25\TEMP%00, REG_EXPAND_SZ
A: TMP, %25SystemRoot%25\TEMP%00, REG_EXPAND_SZ
A: USERNAME, SYSTEM, REG_SZ
A: windir, %25SystemRoot%25%00, REG_EXPAND_SZ
A: NUMBER_OF_PROCESSORS, 2, REG_SZ
A: PROCESSOR_LEVEL, 6, REG_SZ
A: PROCESSOR_IDENTIFIER, ( Intel64 Family 6 Model 47 Stepping 2, GenuineIntel ), REG_SZ
A: PROCESSOR_REVISION, 2f02, REG_SZ
A: JAVA_HOME, C:\Program Files\AdoptOpenJDK\jdk-8.0.265.01-hotspot\, REG_SZ
A: UATDATA, C:\windows\CCM\UATData\D9F8C395-CAB8-491d-B8AC-179A1FE1BE77, REG_SZ

We need an example. If I wanted to define a variable named LICENSE should have value C:\TEMP\LICENSE.TXT, I’d use the following:

Relevance:

not exists value "LICENSE" whose (it as string = "C:\TEMP\LICENSE.TXT") of keys "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" of registry

Action Script:

waithidden reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v LICENSE /t REG_SZ /d "C:\TEMP\LICENSE.TXT" /F

action requires restart "License_Key_Update"
2 Likes