The VBScript Network and Systems Administrator's Cafe:

Environment Variables

Jan 5 2009   2:03AM GMT

Retrieve environment variable values from a remote system with WMI



Posted by: Jerry Lees
WMI, Environment Variables, Win32_Environment, VBScript, Windows Management Interface, System Administration, Remote management, PATH variable

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

Oct 24 2008   12:30AM GMT

Using Environment variables inside a VBScript script



Posted by: Jerry Lees
VBScript, wscript.shell, wscript, Environment Variables

As an extension of the theme I’ve been blogging about lately of “interfacing with other types of applications” in VBScript, I’d like to share with you a snippet of VBScript code that will allow you to read environment variables from your system. This will allow you to determine a great deal of information, since many applications use environment variables for configuration information– and the OS itself does as well, like the server that logged you on is %logonserver%.

There are two types of environment variables:

  • System variables, which are available to every process across the system
  • Process variables, which are only available to the process and disappear when the process is completed. These are sometimes referred to a user environment variables.

You can see examples of each of these environment variables being used below via the “Environment” method of the Wscript.Shell object.
Set WshShell = WScript.CreateObject(”WScript.Shell”)

‘create a Process (user profile) Level environment variable object
Set WshProccessEnv = WshShell.Environment(”Process”)
‘create a System Level environment variable object
Set WshSysEnv = WshShell.Environment(”System”)

‘display a system environment variable
Wscript.Echo WshSysEnv(”NUMBER_OF_PROCESSORS”)
‘display a user profile level environment variable
Wscript.Echo WsProcessEnv(”Path”)

Enjoy!