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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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
Bill PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.