The VBScript Network and Systems Administrator's Cafe

Oct 24 2008   12:30AM GMT

Using Environment variables inside a VBScript script

Jerry Lees Jerry Lees Profile: Jerry Lees

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!

4  Comments 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
  • Genzo
    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
    0 pointsBadges:
    report
  • Genzo
    That should read: WshShell.Run “cmd /c %SDP%sdp$Blahblah.exe /Someflag:flag”,0,true For some reason it removed my backslashes.
    0 pointsBadges:
    report
  • Genzo
    [CODE]WshShell.Run “cmd /c %SDP%sdp$Blahblah.exe /Someflag:flag”,0,true[/CODE]
    0 pointsBadges:
    report
  • Genzo
    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
    0 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: