When populating an system environment variable with the below VBscript, the registry is populated here:
HKEY_USERS\S-1-5-18\Environment
BUT, when I check the environment variable manually, it doesn't show (right-click My Computer, etc).
When running a VBScript to check WMI, the variable is there.

'---------delete------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select * from Win32_Environment Where Name = 'LM_License_File'")

For Each objItem in colItems
objItem.Delete_
Next

'---------recreate---------------
Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_

objVariable.Name = "LM_License_File"
objVariable.UserName = "System"
objVariable.VariableValue = "Test"
objVariable.Put_


When I create the variable manually, the registry is populated here:
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment
and it DOES show in My Computer, etc.
and it DOES show when I run my VBScript checker.

option explicit
dim strComputer, objWMIService, colItems, objItem

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select * from Win32_Environment Where Name = 'LM_License_File'")

For Each objItem in colItems
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "User Name: " & objItem.UserName
Wscript.Echo "Variable Value: " & objItem.VariableValue
Next


The program that needs the variable only sees it when I create it manually.

What am I doing wrong here? I usually handle env vars with MSI, but I'm not using one here.
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
I would add that a reboot is not fixing this issue.

Also, I've been digging into a comment VBScab made awhile back "avoid using System environment variables, as they are not propogated to all processes when they're updated."

You'd think a reboot would sort that out. It's doubly odd that Microsoft suggests creating environment variables via VBS with WMI, but doesn't mention that it doesn't actually work. [:D]
http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar05/hey0318.mspx

I'm going to research creating a user variable and then running the VBS for each user that logs in, but that's an ugly solution in my opinion and I need to check if a locked-down user has rights to create one.
Answered 08/31/2009 by: turbokitty
Sixth Degree Black Belt

Please log in to comment
0
Use the boolean property SystemVariable, don't try to use the system user name.
http://msdn.microsoft.com/en-us/library/aa394143(VS.85).aspx
Answered 08/31/2009 by: aogilmor
Ninth Degree Black Belt

Please log in to comment
0
Actually I figured it out.. there's a typo in the "Scripting Guys" script.

"System" should read "<System>".

It's now working.


'---------delete------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select * from Win32_Environment Where Name = 'LM_License_File'")

For Each objItem in colItems
objItem.Delete_
Next

'---------recreate---------------
Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_

objVariable.Name = "LM_License_File"
objVariable.UserName = "<System>"
objVariable.VariableValue = "Test"
objVariable.Put
Answered 08/31/2009 by: turbokitty
Sixth Degree Black Belt

Please log in to comment
0
strange they'd use that instead of the boolean provided by WMI, but whatever works.
Answered 08/31/2009 by: aogilmor
Ninth Degree Black Belt

Please log in to comment
0
I don't doubt that the boolean is more correct. Thanks. [:)]
Answered 08/31/2009 by: turbokitty
Sixth Degree Black Belt

Please log in to comment
0
I never made an assertion about what's more correct, just making an observation. Why insert a username if the function provides a property that toggles system on or off? I'll take a documented property over what 2 guys on the internet wrote. Even the scripting guys. But, you go with whatever warms your cockles [:D]
Answered 08/31/2009 by: aogilmor
Ninth Degree Black Belt

Please log in to comment
0
no need to bring my cockles into this. [:D]
Answered 08/31/2009 by: turbokitty
Sixth Degree Black Belt

Please log in to comment
0
Oh gads my reading comprehension sux today. I thought you said you doubt the boolean is more correct. Good thing I'm a good humored bloke I could have said something even dumber. [:)]
Answered 08/31/2009 by: aogilmor
Ninth Degree Black Belt

Please log in to comment
0
Turbs,

To save the engine having to repeatedly access an object's resources, you might want to get into the habit of using the 'With' keyword. Thus:objVariable.Name = "LM_License_File"objVariable.UserName = "<System>"
objVariable.VariableValue = "Test"
objVariable.Put
becomes: With objVariable
.Name = "LM_License_File"
.UserName = "<System>"
.VariableValue = "Test"
.Put
End With
Answered 09/01/2009 by: VBScab
Red Belt

Please log in to comment
Answer this question or Comment on this question for clarity