Jump to content

Recommended Posts

Posted

I've seen a few threads on adding Environment Variables using VBScript but I can't seem to find information on retrieving a particular Variable and then changing the value.

 

 

Any ideas?

Posted

This is a rather comprehensive list of methods to add, remove and change them using vbs

 

Add, Remove, or Retrieve Environment Variables

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
'Created by: Rob Olson - Dudeworks 
'Created on: 10/17/2001 
'Purpose: Get Environment Variables. 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

wscript.echo "Working with the Environment: Provided by www.dudeworks.net"&vbcrlf&vbcrlf&strval 

'// Create an instance of the wshShell object
set WshShell = CreateObject("WScript.Shell")
'Use the methods of the object
wscript.echo "Environment.item: "& WshShell.Environment.item("WINDIR")
wscript.echo "ExpandEnvironmentStrings: "& WshShell.ExpandEnvironmentStrings("%windir%")

'// add and remove environment variables
'// Specify the environment type ( System, User, Volatile, or Process )
set oEnv=WshShell.Environment("System")

wscript.echo "Adding ( TestVar=Windows Script Host ) to the System " _
& "type environment"
' add a var
oEnv("TestVar") = "Windows Script Host"

wscript.echo "removing ( TestVar=Windows Script Host ) from the System " _
& "type environment"
' remove a var
oEnv.Remove "TestVar"


'// List all vars in all environment types

'//System Type
set oEnv=WshShell.Environment("System")
for each sitem in oEnv 
strval=strval & sItem &vbcrlf 
next
wscript.echo "System Environment:"&vbcrlf&vbcrlf&strval 
strval=""

'//Process Type
set oEnv=WshShell.Environment("Process")
for each sitem in oEnv 
strval=strval & sItem &vbcrlf 
next
wscript.echo "Process Environment:"&vbcrlf&vbcrlf&strval 
strval=""

'//User Type
set oEnv=WshShell.Environment("User")
for each sitem in oEnv 
strval=strval & sItem &vbcrlf 
next
wscript.echo "User Environment:"&vbcrlf&vbcrlf&strval 
strval=""

'//Volatile Type
set oEnv=WshShell.Environment("Volatile")
for each sitem in oEnv 
strval=strval & sItem &vbcrlf 
next

wscript.echo "Volatile Environment:"&vbcrlf&vbcrlf&strval 
strval=""

  • Thanks 1
  • 3 weeks later...
Posted

Thanks for your help with that, I managed to create a script to do what I needed doing. A snippet is below in case anyone wants to do a similar thing. Just to clarify, we use the Location variable to determine which printers we want that PC to be connected to. The connection is achieved via a script that looks at the Location variable.

 

 

set WshShell = CreateObject("WScript.Shell")

set oEnv=WshShell.Environment("System")
set oPro=WshShell.Environment("Process")

dim cn, result

cn = cstr(oPro("COMPUTERNAME"))
result = left(cn,4)
IF result = "IT-O" then
oEnv("LOCATION") = "IT"
elseif result = "ART-" then
oEnv("LOCATION") = "ART"
elseif result = "LRC" then
oEnv("LOCATION") = "LIBRARY"

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...