Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript to specifically change Windows XP environment variable 2

Status
Not open for further replies.

geranimo666

Technical User
Sep 19, 2006
195
US
Hello all-

I didn't know that in Windows XP you can't just use the ping or ipconfig commands if you simply drop to a command prompt. You must first change the path to c:\windows\system32 in order to do so. I check the environment variable that is set for %comspec% and it's c:\windows\system32.. I have users across my entire building that have xp which is not a big deal for the user but if a technician needs to get to any of these machines, they'd need to change to that path in order to ping, ipconfig etc. Is there a vbscript I could run within a GPO or login script that somehow modifies this?

This was never a problem in Windows 2000 Pro as long as you typed cmd from Start/Run.. you could use these commands. Perhaps I'd need the script to copy these two exe's ping and ipconfig to the root c: drive instead?

If anyone has any ideas, I would be greatful indeed!!
geranimo
 
Ping and ipconfig work fine on my XP box and on thousands of others ...
 
Really?

So you just type cmd from Start/run and then you type in ping or ipconfig and it works.. just curious, when you drop to the command prompt, what is your path.. is it c:\windows\system32 or c:\

just curious since you say it works for you

thanks
geranimo
 
A classical XPpro %PATH% value:
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Executing IPCONFIG from a RUN command will typically open adn immediately close a command prompt.

Instead of just typing IPCONFIG at the run use cmd.exe /k ipconfig

If you would like to use the old GUI interface WinIPCfg, you can download the version that works on XP from here:

The EXE name is different but can be renamed to WinIPCfg.exe if you are like me and a creature of habbit. I always like to copy this down to each of my workstations. The utility will work with both Windows 2000 and XP workstations. It does not work with Vista.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
thanks everyone for the great responses

If I chose to use the C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem

is there a way to place this in a login script or vbscript overall, I have an IT OU I could test with my colleagues before deploying to 300 users.. just want to see if this can be done..

thanks eitherway for the great responses

geranimo
 
>So you just type cmd from Start/run and then you type in ping or ipconfig and it works

Yes

>when you drop to the command prompt, what is your path.. is it c:\windows\system32 or c:\

Doesn't matter what my current directory is, the commands still run quite happily.

The environment variable you actually want to be looking at is the PATH variable - but it should contain the necessary entries unless someone has edited it (either by accident or maliciously)
 
actually no, it doesn't work.

I changed the environment variable %comspec% to reflect the longer path with semicolons mentioned below but when I type cmd from Start/Run.. it brings me as a default to the P:>_ drive, so then I change to C: but it brings me to the root of C: so when I type in ping or ipconfig it doesn't recognize the commands.. I then need to change to c:\windows\system32 and only then will my ping and/or ipconfig command work..

anyone have any suggestions? could it be the image on the XP pcs? or a group policy? I don't think it would be a GPO affecting this but thought I'd mention in case it sparked anyones interest.

thanks
geranimo
 
if you want to specificially execute a dos command (loosly speaking) i would always recommned wrapping it with "cmd /c" first (or use the environmentvariable %comspec% if it can be relied up)
then you want to start an exe or something, so if its location is not in the PATH statement or you cant rely on the PATH statement then you will have to specify the full path to the exe.

you could wrap all of these attempts in err trapping

1. try ping.exe
2. try %comspec% ping.exe
3. try cmd /c ping.exe
4. try cmd /c c:\windows\system32\ping.exe
5. go off and find where ping.exe is then have that in the .run command

etc etc, this is based on waiting for errors to happen.
or you can defensive program...

1. check what %comspec% is set to
2. check the %path% statement

etc etc
 
<sigh> %comspec% is not the PATH environment variable. Surprisingly, %PATH% is.

If %PATH% is set correctly then it doesn't matter what drive Start/Run/cmd takes you to.
 
strongm-

that worked great! I can now issue these commands from any drive letter !

Is there a way for me to put this in a GPO or vbscript in order to send it out to all of my XP pc users?

Thanks again - time for a star!!

geranimo
 
all of you machines should have those PATH statements setup already..

i tend to update the registry directly when mod'in environment variables. not my best script but there you go

you will of course have to start a new thread if you want the new envvar to have taken effect...on some OS;s i still think reboots are required...or should i say windows needs to be sent a little 'you need to update' message

'author: richard moyle
'date: 02/01/2007
'desscription: script to update/add system env variables

Option Explicit
'On Error Resume Next
Dim intError

Call Main()
If intError <> 0 Then
Wscript.Quit intError
Else
Wscript.Quit Err.Number
End If

'#################################
Sub Main()
Dim WshShell, strVarName, strValue, strTemp

If Wscript.Arguments.Count < 2 Then
Wscript.Quit 1
End If

'get the value
If Wscript.Arguments.Named.Exists("value") Then
strValue = Wscript.Arguments.Named.Item("value")
Else
Wscript.Quit 1
End If
'get the name
If Wscript.Arguments.Named.Exists("name") Then
strVarName = Wscript.Arguments.Named.Item("name")
Else
Wscript.Quit 1
End If

Set WshShell = CreateObject("Wscript.Shell")

On Error Resume Next
strTemp = WshShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\" & strVarName)
On Error Goto 0
If strTemp = "" Then
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\" & strVarName, strValue, "REG_SZ"
Else
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\" & strVarName, strTemp & ";" & strValue, "REG_SZ"
End If


Set WshShell = Nothing

End Sub
 
NB scratch last post the script seems to have broken my environment variables :) oh well
 
thx mrmovie,

actually the XP machines I've seen at this customer site have a path statement but is not right. it looks as though the %comspec% variable is the only one holding the c:\windows\system32 path and that is the problem that started this forum thread for me.

now by adding the path statement with the new path below, I can ping from anywhere, any drive letter per say..

I'll try your script within a test ou -thanks!

geranimo
 
mrmovie-

sorry for the novice question but where in the script can I add my %PATH% c:\windows\system32;c:\windows;c:\windows\system32\Wbem

thanks for any support

geranimo
 
you start the script with /name:path /value:C:\test

and it will do the rest....but dont use it as it breaks my machine!!! lol
 
i really dont recall why i ignored this in the past and went for the registry, i know i looked at it as the code was commented out...


Dim WshSySEnv, WshShell
Set WshShell = CreateObject("Wscript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
'WshSysEnv("Path") = "123"
Msgbox WshSysEnv("Path")
strTemp = WshSysEnv("Path")
WshSysEnv("Path") = strTemp & ";c:\rich"
Set WshSySEnv = Nothing
Set WshShell = Nothing
 
rrr, i seem to recall that i was originally trying to create an environment variable...which the WshSysEnv method wont allow...but seems ok to update certain variables...
 
hopefully this will stop messing around!!!

' This code creates a new system environment variable called FOOBAR.
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strVarName = "FOOBAR"
strVarValue = "Foobar Value"
strComputer = "."
' ------ END CONFIGURATION ---------
set objVarClass = GetObject("winmgmts:\\" & strComputer & _
"\root\cimv2:Win32_Environment")
set objVar = objVarClass.SpawnInstance_
objVar.Name = strVarName
objVar.VariableValue = strVarValue
objVar.UserName = "<SYSTEM>"
objVar.Put_
WScript.Echo "Created environment variable " & strVarName

 
this seems to work fairly well
start it with something like

cscript.exe scriptname.vbs /name:varnamehere /value:valuehere /username:SYSTEM



'author: richard moyle
'date: 02/01/2007
'desscription: script to update/add system env variables

Option Explicit
'On Error Resume Next
Dim intError

Call Main()
If intError <> 0 Then
Wscript.Quit intError
Else
Wscript.Quit Err.Number
End If

'#################################
Sub Main()
Dim strVarName, strValue, strTemp, strUserName, blnOverWrite
Dim blnFound

If Wscript.Arguments.Count < 2 Then
Wscript.Quit 1
End If

'get the value
If Wscript.Arguments.Named.Exists("value") Then
strValue = Wscript.Arguments.Named.Item("value")
Else
Wscript.Quit 1
End If
'get the name
If Wscript.Arguments.Named.Exists("name") Then
strVarName = Wscript.Arguments.Named.Item("name")
Else
Wscript.Quit 1
End If
'get the username
If Wscript.Arguments.Named.Exists("username") Then
strUserName = Wscript.Arguments.Named.Item("username")
If Left(strUserName, 1) <> "<" Then
strUserName = "<" & strUserName
End If
If Right(strUserName, 1) <> ">" Then
strUserName = strUserName & ">"
End If
Else
Wscript.Quit 1
End If
blnOverWrite = False
blnFound = False
'overwrite
If Wscript.Arguments.Named.Exists("overwrite") Then
blnOverWrite = True
End If

Dim objWMI, colVars, aVar, objVarClass

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colVars = objWMI.InstancesOf("Win32_Environment")
For Each aVar in colVars
If UCase(strVarName) = UCase(aVar.Name) Then
Wscript.Echo "we have a match on name " & aVar.Name
If UCase(strUserName) = UCase(aVar.UserName) Then
'ok we have a match,,therefore we need to update it
Wscript.Echo "we have a match on username " & aVar.UserName & " as well"
If blnOverWrite = True Then
'sgbox "overwrite"
aVar.variableValue = strValue
aVar.Put_
Else
aVar.variableValue = aVar.variableValue & ";" & strValue
aVar.Put_
End If
blnFound = True
Exit For
End If
End If
Next
Set colVars = Nothing
Set objWMI = Nothing

If blnFound = False Then
'perhaps it doesnt exist?
Set objVarClass = GetObject("winmgmts:\\.\root\cimv2:Win32_Environment")
Set aVar = objVarClass.SpawnInstance_
aVar.Name = strVarName
aVar.VariableValue = strValue
aVar.UserName = strUserName
aVar.Put_
Set aVar = Nothing
Set objVarClass = Nothing
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top