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.
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.
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.
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)
Please log in to answer
Posted by:
turbokitty
14 years ago
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.
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
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
http://msdn.microsoft.com/en-us/library/aa394143(VS.85).aspx
Posted by:
turbokitty
14 years ago
Actually I figured it out.. there's a typo in the "Scripting Guys" script.
"System" should read "<System>".
It's now working.
"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
Posted by:
aogilmor
14 years ago
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:
aogilmor
14 years ago
Posted by:
anonymous_9363
14 years ago
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:
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.
so that the conversation will remain readable.