/build/static/layout/Breadcrumb_cap_w.png

Environment Variable question

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

Answers (9)

Posted by: turbokitty 14 years ago
6th Degree Black Belt
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.
Posted by: aogilmor 14 years ago
9th Degree Black Belt
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
Posted by: turbokitty 14 years ago
6th Degree Black Belt
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
Posted by: aogilmor 14 years ago
9th Degree Black Belt
0
strange they'd use that instead of the boolean provided by WMI, but whatever works.
Posted by: turbokitty 14 years ago
6th Degree Black Belt
0
I don't doubt that the boolean is more correct. Thanks. [:)]
Posted by: aogilmor 14 years ago
9th Degree Black Belt
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]
Posted by: turbokitty 14 years ago
6th Degree Black Belt
0
no need to bring my cockles into this. [:D]
Posted by: aogilmor 14 years ago
9th Degree Black Belt
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. [:)]
Posted by: anonymous_9363 14 years ago
Red Belt
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
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ