The VBScript Network and Systems Administrator's Cafe

Oct 24 2008   12:30AM GMT

Using Environment variables inside a VBScript script



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

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!

Comment on this Post

Leave a comment:

Genzo  |   Jun 27, 2011  5:25 PM (GMT)

How would I use the above to call a variable called %SDP% that I use? I’d like to do something like WshShell.Run “cmd /c %SDP%sdp$Blahblah.exe /Someflag:flag”,0,true


 

Genzo  |   Jun 27, 2011  5:36 PM (GMT)

That should read: WshShell.Run “cmd /c %SDP%sdp$Blahblah.exe /Someflag:flag”,0,true

For some reason it removed my backslashes.


 

Genzo  |   Jun 27, 2011  5:38 PM (GMT)

[CODE]WshShell.Run “cmd /c %SDP%sdp$Blahblah.exe /Someflag:flag”,0,true[/CODE]


 

Genzo  |   Jun 27, 2011  5:42 PM (GMT)

hmm, *newb aura*. Can’t get it to do backslashes…

WshShell.Run “cmd /c %SDP%sdp(BACKSLASH)$Blah(BACKSLASH)blah.exe /Someflag:flag”,0,true