Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing windows environment variables on remote computers

139 views
Skip to first unread message

Peter

unread,
Oct 23, 2003, 11:52:25 AM10/23/03
to
Everyone,

I am trying to write a script to change a Windows
environment variable on a bunch of computers. I have the
following VB Script, however it only makes the change on
the local computer. What am I doing wrong? Thanks in
advance!

Const ForReading = 1
dim objWMIService, colAdapters, StrComputer, objAdapter,
orcachange, strcurrentvalue

Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("c:\servers.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop


For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objWMIService = GetObject("winmgmts:\\" & StrComputer
&"\root\cimv2")
set objshell = createobject("wscript.shell")
set oracchange = objshell.environment("SYSTEM")

strcurrentvalue = oracchange("TNS_ADMIN")
oracchange("TNS_ADMIN") = "\\\\thor\\TNS_ADMIN_GENERAL$"


Next

Tom Lavedas

unread,
Oct 23, 2003, 12:46:12 PM10/23/03
to
You switched horses in midstream, i.e. Yyu started with
WMI (which can doe the job) and then switched to WSH
(which is restricted to local operation).

Try something like this instead (adapted from the MSDN
documentation for WMI's Win32_Environment call) ...

Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set PathVariables = objWMIService.ExecQuery( _
"Select * from Win32_Environment where " _
& "Name = 'Path' and Username='<system>'" )
NewEnvValue = ";%UserProfile%\Script;" _
& "%UserProfile%\Script\Testing"
For Each EnvVar in PathVariables
sPath = EnvVar.VariableValue
if Instr(1, sPath, NewEnvValue, vbTextCompare) = 0 Then
EnvVar.VariableValue = sPath & NewEnvValue
EnvVar.Put_
End if
wsh.echo EnvVar.VariableValue
Next
Next

Tom Lavedas
===========

>.
>

0 new messages