Welcome to MSDN Blogs Sign in | Join | Help

Rahul Soni's blog

Never assume the obvious is true!

News



  • These postings are provided "AS IS" with no warranties, and confers no rights.
How to read User and System Variables using VBScript

Quite easy!

Dim objWSH
Dim objUserVariables
Dim objSystemVariables

Set objWSH =  CreateObject("WScript.Shell")
'This actually returns all the User Variables, and you either loop through all, or simply print what you want
Set objUserVariables = objWSH.Environment("USER")
MsgBox(objUserVariables("TEMP"))

'This returns all the System Variables, and you either loop through all, or simply print what you want
Set objSystemVariables = objWSH.Environment("SYSTEM")
MsgBox(objSystemVariables("PATH"))

'Say you want to add a System or a User variable. Pretty simple, just use the object and assign the value!
objSystemVariables("I_AM_SYSTEM_VARIABLE") = "System Variable Sir!"
objUserVariables("I_AM_USER_VARIABLE") = "User Variable Sir!"

Remember, if you want to do this in classic ASP (on IIS 6), it will work only if you are running the App Pool under "Local System" or you have a custom account with Admin rights. It is not gonna work with Network Service!

Hope this helps!
Rahul

Posted: Friday, December 29, 2006 12:10 PM by rahulso
Filed under:

Comments

PhaniRajuYN said:

Great post !  However, I vaguely remember that the USER profile does not get loaded in IIS6 due to security constraints .

So the "USER" environment variables are not gonna be enumerated in IIS 6

# January 10, 2007 11:22 AM

rahulso said:

Honestly speaking, IMHO, playing with Environment variables in a web application is not a decent idea anyways!

# January 11, 2007 7:00 AM

Ramani Kandula said:

It is a Great Post. It is vert helpful to me

# August 10, 2007 1:04 PM

Azhar said:

Nice work man

ur code helped me and its working properly :-)

# September 11, 2007 6:39 AM

Dustin said:

In regards to saying that web apps using Env variables is not a good idea is terribly wrong.  I use HTA's in a lot of scripts that i write to automate network tasks and the use of environment variables in HTA's is not only much more standardized, but much more useful. Web apps aren't the only place these are used :)1

# December 9, 2007 1:38 PM

rahulso said:

Hello Dustin,

Thanks for your comment. I guess, I never said that Web apps are the only place where these variables are used. I just wanted to ensure that I am not asking folks to run application under Local System account just to read those variables.

Besides, I have a difference of opinion here and would rather stick to the fact that there might be applications that use Env variables. BUT, IMHO... that's not a very good practice, specially if you end up changing the app pool identity to Local System :-)

I am yet to encounter a scenario where a specific requirement cannot be achieved from a web app WITHOUT using ENV variables!

# December 10, 2007 12:43 AM

Aaron said:

Thanks...  this solved my problem.

# February 28, 2008 1:59 PM

Sandeep Applabs said:

Thanks Our Issue got Resolved by this post...

# March 7, 2008 1:35 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker