Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Find all Env Variables

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3425
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Find all Env Variables
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Find all Env Variables - 6/16/2005 11:59:42 PM   
  Misery

 

Posts: 7
Score: 0
Joined: 6/16/2005
From:
Status: offline
I'd like to know if there is any way to get all the environment variables and their corresponding value in a computer using VBScript. Apart from the System defined variables, I'd also like to find user defined environment variables. Thanks!
 
 
Post #: 1
 
 Re: Find all Env Variables - 6/18/2005 2:30:12 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Maybe this can help you :

' Remarks
' The WshEnvironment object is a collection of environment variables that is returned by the WshShell object's Environment property.
' This collection contains the entire set of environment variables (those with names and those without).
' To retrieve individual environment variables (and their values) from this collection, use the environment variable name as the index.

' Example
' The following code displays an environment variable.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
WScript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")

I understand that for SYSTEM you could use : PROCESS or USER or VOLATILE.

Good luck !

< Message edited by didorno -- 7/20/2005 6:25:51 AM >

(in reply to Misery)
 
 
Post #: 2
 
 Re: Find all Env Variables - 6/18/2005 4:46:33 AM   
  Misery

 

Posts: 7
Score: 0
Joined: 6/16/2005
From:
Status: offline
Thank didorno.
My problem in fact is that I may not know all the environment variable names existed in the computer. Is there any possible way to get all the enviroment variable names, such that I can call the method you suggested to retrieve their respective values?

(in reply to Misery)
 
 
Post #: 3
 
 Re: Find all Env Variables - 6/19/2005 5:07:56 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Sorry Misery, I did read your post not well.
I think you can look (or read with a script) in the registry to get the environmental variables.
For me (Windows Home XP SP2) the locations are
HKEY_CURRENT_USER\ENVIRONMENT
HKEY_CURRENT_USER\VOLATILE ENVIRONMENT
HKEY_LOCAL_MACHINE\SYSTEM\CURRENTCONTROLSET\CONTROL\SESSION MANAGER\ENVIRONMENT.

For reading the registry you can use the next approach

const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "System\CurrentControlSet\Control\Session Manager\Environment"
oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes

For i = 0 To UBound(arrValueNames)
StdOut.WriteLine "File Name: " & arrValueNames(i) & " -- "
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath, arrValueNames(i),strValue
StdOut.WriteLine "Location: " & strValue
StdOut.WriteBlankLines(1)
Next


' Source : The System Administration Scripting Guide, part of the Windows .NET Server Resource Kit.

Good luck !

< Message edited by didorno -- 7/20/2005 6:26:59 AM >

(in reply to Misery)
 
 
Post #: 4
 
 Re: Find all Env Variables - 6/19/2005 5:42:45 PM   
  Misery

 

Posts: 7
Score: 0
Joined: 6/16/2005
From:
Status: offline
Thank you didorno, my problem solved.

(in reply to Misery)
 
 
Post #: 5
 
 Re: Find all Env Variables - 6/19/2005 10:08:01 PM   
  Misery

 

Posts: 7
Score: 0
Joined: 6/16/2005
From:
Status: offline
I have one more question with regard to the solultion suggested by didorno.

When I run the following code,

oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath, arrValueNames(i),strValue

I get back a full path for those values of environment variables in whichever form, including the one in REG_EXPAND_SZ. I'd like to know if there is any way to change the above code, such that I can get back the short form for REG_EXPAND_SZ type value. Instead of returning C:\Windows, I'd like to get back %SystemRoot%. Is there any ways to do so?

Thank you in advance!

(in reply to Misery)
 
 
Post #: 6
 
 Re: Find all Env Variables - 6/20/2005 7:41:14 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
If you modify the next code you can use the strings to obtain your
wish, I think.

set WshShell = WScript.CreateObject("WScript.Shell")

Set WshSysEnv = WshShell.Environment("SYSTEM")
WScript.Echo "System"
For Each Env in WshSysEnv
WScript.Echo Env
next
Set WshSysEnv = WshShell.Environment("PROCESS")
WScript.Echo vbCrLf & "Process"
For Each Env in WshSysEnv
WScript.Echo Env
next
Set WshSysEnv = WshShell.Environment("USER")
WScript.Echo vbCrLf & "User"
For Each Env in WshSysEnv
WScript.Echo Env
next
Set WshSysEnv = WshShell.Environment("VOLATILE")
WScript.Echo vbCrLf & "Volatile"
For Each Env in WshSysEnv
WScript.Echo Env
next


Good luck !

< Message edited by didorno -- 7/20/2005 6:27:50 AM >

(in reply to Misery)
 
 
Post #: 7
 
 Re: Find all Env Variables - 6/20/2005 1:22:50 PM   
  Misery

 

Posts: 7
Score: 0
Joined: 6/16/2005
From:
Status: offline
Thanks again didorno, but my problem is that I'd like to get the environment variables from a network server. If I use the method you suggested, I guess I can only get the values from my computer which runs the script.

I can use the first method you suggested to retrieve all the environment variables from the network server by changing "." to the name of the server. But I cannot get the non-expanded form of the values of environment variables. Do you have any suggestion?

(in reply to Misery)
 
 
Post #: 8
 
 Re: Find all Env Variables - 6/21/2005 12:32:06 AM   
  mbouchard


Posts: 1922
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
This can be done with WMI,

Taken from Script Center.


      

(in reply to Misery)
 
 
Post #: 9
 
 Re: Find all Env Variables - 6/21/2005 5:36:54 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
mbouchard, nice code, compact and easy.
I can use that too.
Thank you.

(in reply to Misery)
 
 
Post #: 10
 
 Re: Find all Env Variables - 6/21/2005 6:01:30 AM   
  mbouchard


Posts: 1922
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Thanks, but I had nothing to do with it except for copy and paste.

(in reply to Misery)
 
 
Post #: 11
 
 Re: Find all Env Variables - 6/21/2005 7:51:35 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
mbouchard, that is what I do most of the time !
Plus a few minor adaptations. In fact, I learn everything from
examples and modifying them. And a lot of trial and error.......
You know what I mean.
I'm thinking about a way to be able to show all possible underlying commands one level lower than f.i.

      
So, how can I show all WshSysEnv.?????????? properties and methods ?

Well, maybe another topic.

(in reply to Misery)
 
 
Post #: 12
 
 Re: Find all Env Variables - 6/21/2005 1:20:51 PM   
  Misery

 

Posts: 7
Score: 0
Joined: 6/16/2005
From:
Status: offline
Thank you mbouchard!
I can get what I want to achieve now.

(in reply to Misery)
 
 
Post #: 13
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Find all Env Variables Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts