Create or Modify an Environmental Variable with a script

Hi

Was looking to create create an environmental variable to point to a licence server, however the script overwrites the existing values.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_

objVariable.Name = "LM_Licence_File"
objVariable.UserName = "<System>"
objVariable.VariableValue = "27004@servername"
objVariable.Put_

Is there a way to set it so that if the variable name already exists (in this case LM_Licence_File)it adds to it (except when the value already exists)
kwatt562Asked:
Who is Participating?
 
Bill PrewCommented:
And I guess the cleaner way to do that would be:

Set objShell = CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("SYSTEM")

If objEnv("LM_Licence_File") <> "" Then
   objEnv("LM_Licence_File") = objEnv("LM_Licence_File") & ",27004@servername"
Else
   objEnv("LM_Licence_File") = "27004@servername"
End If

Open in new window


Keep in mind that you will need Admin rights to set SYSTEM variables.

~bp
0
 
Bill PrewCommented:
Have you tried this, it should set the variable permanently:

Set objShell = CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("SYSTEM")
 objEnv("LM_Licence_File") = "27004@servername"

Open in new window

~bp
0
 
Bill PrewCommented:
Or did I misunderstand the question, do you want to concatenate a new value if it's already set?  In that case something like:

Set objShell = CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("SYSTEM")
objEnv("LM_Licence_File") = objEnv("LM_Licence_File") & ",27004@servername"

Open in new window

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.