The VBScript Network and Systems Administrator's Cafe

Jan 5 2009   2:03AM GMT

Retrieve environment variable values from a remote system with WMI

Jerry Lees Jerry Lees Profile: Jerry Lees

Back in October I shared with you a way to retrieve environment variables with a script titled Using Environment variables inside a VBScript script. The script worked like a champ and got me through several tight binds in gathering information, hopefully it has for you as well. However, it has one flaw… it doesn’t work on a remote system! In fact, it only works on the system that the script is running on.

So, I had to recently go back to the drawing board and create a script and series of functions that would work remotely so that I could wrap it in a function to gather information from a bunch of servers. I also Learned a thing or two in the process!

First, The script uses the Win32_environment classes in WMI and this class returns ALL envirnment variables for ALL users. That threw me for a loop when I saw multiple PATH environment variables– some that were different. Second, The search is case sensitive without using the SQL % wildcards (I didn’t need them, so implementing that is left up to you the reader.)

I have the three functions I wrote, that are really variations of one another below. Just add them to your script and call them with the appropriate values and you should be in business!

Function RemoteAllEnvironmentVariables(ServerName)
     ‘Lists all environment variables on the remote system
    
Dim objWMIService, colItems, objItem
 
     RemoteAllEnvironmentVariables = “”
     Set objWMIService = GetObject(“winmgmts:\\” & ServerName & “\root\cimv2″)
     Set colItems = objWMIService.ExecQuery(“Select * from Win32_Environment”)
     For Each objItem in colItems
         RemoteAllEnvironmentVariables = RemoteAllEnvironmentVariables & objitem.name & “: ” & objItem.VariableValue & VbCrLf
     Next
End Function

Function RemoteAllEnvironmentVariablesVariations(ServerName, VariableName)
     ‘Lists all environment variables on the remote system, along with every case variation
     Dim objWMIService, colItems, objItem
 
     RemoteAllEnvironmentVariablesVariations = “”
     Set objWMIService = GetObject(“winmgmts:\\” & ServerName & “\root\cimv2″)
     Set colItems = objWMIService.ExecQuery(“Select * from Win32_Environment”)
     For Each objItem in colItems
         If UCase(objItem.name) = UCase(VariableName) Then
         RemoteAllEnvironmentVariablesVariations = RemoteAllEnvironmentVariablesVariations & objitem.name & “: ” & objItem.VariableValue & VbCrLf
         End If
     Next
End Function

 
Function RemoteEnvironmentVariable(ServerName, VariableName)
     ‘Lists all environment variables on the remote system, exactly as you typed it in the function call.
     Dim objWMIService, colItems, objItem
 
     RemoteEnvironmentVariable = “”
     Set objWMIService = GetObject(“winmgmts:\\” & ServerName & “\root\cimv2″)
     Set colItems = objWMIService.ExecQuery(“Select * from Win32_Environment Where Name = ‘” & VariableName & “‘”)
     For Each objItem in colItems
         RemoteEnvironmentVariable = objItem.VariableValue
     Next
End Function

1  Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Sivalanka
    This post was very helpful for me to retrieve the environment variables remotely. Thank you Jerry. I am now looking for another good post on "How to set windows environment variables remotely" Would appreciate if you can post a solution you know of. Thanks, Sudhakar
    5 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: