Link to home
Start Free TrialLog in
Avatar of curt2000
curt2000

asked on

Append to system path environment variable via login script

I have 400 machines in my domain environment.  We're using a VBS login script which is called via group policy.

I need to append (not replace) a directory path to the existing system path environment variable on each PC.  In other words, I can't mess up the existing path info a user has, but just add on to it.  New machines are added or rebuilt each day, so this needs to be an ongoing thing and not just a one-time push.

Currently I have this working by using the following commands in the script:

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
WshEnv("Path") = WshEnv("Path") & ";M:\DB\whatever\"

The problem is that on some of the machines, not all, the new path keeps getting added on multiple times with each successive login.  On one machine I have the same path appended 11 times and growing!

My question is how can I check for a previous instance of the same path I'm trying to add and then NOT add it if it is already there?  Or, check for a previous instance(s) and then delete them before appending again?


SOLUTION
Avatar of 97WideGlide
97WideGlide

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, hadn't hit submit there for a while.... same sort of answer as first one...
Avatar of curt2000
curt2000

ASKER

Thanks for the fast responses guys.  

I'm not much of a programmer yet, so I don't quite understand what each block of code is doing, but I'll experiment.

Steve - are you thinking that your code example is all I'll need to do the job?  I'll try it out.

Thanks again for the help.
Steve,

Your script is working great!

Now, if I would've used this more intelligent script in the first place I wouldn't have duplicate entries in the system path.

It's probably not a big deal, but how complex is the code to "clean up" duplicate entries in the system path?  It would only be looking specifically for "M:\db\whatever" entries and not anything else (to hopefully make it more simple).
OP, don't sell yourself short.  Your script is essentially the same solution.  The only difference is that before appending another copy of your path to the existing path you just check to see if it has already been appended (exists there) --- just one 'if' statement.


To answer your question regarding cleaning up system path take a look at this page :

http://www.aivosto.com/vbtips/instr.html

It describes in pretty good detail how to use InStr() .  There's a simple way to find out exactly where your string begins in the path.  From there is it simple to remove that portion (read: SUBSTRING)  from your path.  

If you don't want to do the heavy lifting though, I'm sure curt2000 can provide a good script 4 U.

Good Luck.
Although the script to remove is going to be almost identical to what you already have.  Instead of appending your path you will be taking a substring of your system's path.  And you might want to wrap it in a while() loop in order to remove multiple occurrences.

Here's a good link :

http://www.pctools.com/guides/scripting/id/25/?act=reference

Take a look at Replace() and Mid()
Run this code to clean up all addpath entries that are in the beginning or middle for the next couple of days.

OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")


Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
OldPath=WshEnv("Path")
Addpath="M:\db\whatever"
 
OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")
 
if instr(lcase(Oldpath),lcase(addpath))=0 then
  WshEnv("Path") = OldPath & ";" & AddPath
end if

Open in new window

Thanks AmazingTech.  I've been running the script with OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", ""), but it doesn't seem to be removing entries at the beginning, middle, or end.

Am I doing something wrong?  I copied the entire 10 line script for the test.
Thanks for the helpful links 97WideGlide.  I'm going to study up an try to learn something new.
Sorry your original post has it like this "M:\DB\whatever\" to remove both variations from the beginning or middle. I'm not worrying about the end since you actually want the value to be there anyways. Just clearing the dups. If is wasn't at the end the script will add it back anyways.

Addpath="M:\db\whatever\"
OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")

Addpath="M:\db\whatever"
OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")
My current path is showing:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;M:\DB\whatever\;C:\Program Files\QuickTime\QTSystem\

My script is written like this (I left off the portion that appends the path for troubleshooting):

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
OldPath=WshEnv("Path")
Addpath="M:\DB\whatever\"
OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")

If I run the script it doesn't remove M:\DB\whatever\ so I'm just wondering if I type something up wrong.


Again, I'm not a VB guy but you might try escaping the \  as in :

Addpath="M:\\DB\\whatever\\"

HIH
Thanks HIH.  I tried it out, but it didn't work.

I'm studying up on the links you provided too.  I'm thinking I'll need to use some sort of while() command like you were saying along with Replace() and Mid().

The first half of Steve's command definitely finds the new path regardless of where it sits in the string:

if instr(lcase(Oldpath),lcase(addpath))=0 then
  WshEnv("Path") = OldPath & ";" & AddPath

If I could just figure out how to say "remove any instances from the string" I think I could remove the duplicates:

e.g.

if instr(lcase(Oldpath),lcase(addpath))=(any number of times) then
  WshEnv("Path") = OldPath (minus) ";" (minus) AddPath

My problem is I don't know how to tell the computer that.  :-)
What does the wscript.echo come back with?
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
OldPath=WshEnv("Path")
wscript.echo oldpath
Addpath="M:\DB\whatever\"
OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")
wscript.echo oldpath

Open in new window

I check my system path in the registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

and it says this:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;M:\DB\whatever\;C:\Program Files\QuickTime\QTSystem\

I ran your echo script.  The first echo came back with this:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;M:\DB\whatever\;C:\Program Files\QuickTime\QTSystem\

The second echo came back with this:

%systemroot%\system32;%systemroot%;%systemroot%\system32\wbem;c:\program files\quicktime\qtsystem\

It looks like your script is working, but just not writing the information back to the computer for some reason.
Have been away for a while there, looks like you've got it all under control but will keep an eye out if you need any more input from here....

Steve
To write it back we need this:

WshEnv("Path") = OldPath

Oh I see now.
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
OldPath=WshEnv("Path")
wscript.echo oldpath
Addpath="M:\DB\whatever\"
OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")
wscript.echo oldpath
if instr(lcase(Oldpath),lcase(addpath))=0 then OldPath = OldPath & ";" & AddPath
 
WshEnv("Path") = OldPath & ";" & AddPath

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I think we're getting close, but the script above takes this path:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;M:\DB\whatever\;C:\Program Files\QuickTime\QTSystem\

and turns it into this path:

%systemroot%\system32;%systemroot%;%systemroot%\system32\wbem;c:\program files\quicktime\qtsystem\;m:\db\whatever\;%systemroot%\system32;%systemroot%;%systemroot%\system32\wbem;c:\program files\quicktime\qtsystem\

:-)
Thanks for the help Steve.  We're just trying to get duplicates to be removed from the path now.  I'd appreciate any help!
Using this can you post each of the echos?

We'll rem out the actual writing to the system for now.
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
OldPath=WshEnv("Path")
wscript.echo oldpath
Addpath="M:\DB\whatever\"
OldPath=Replace(lcase(OldPath), lcase(AddPath) + ";", "")
wscript.echo oldpath
if instr(lcase(Oldpath),lcase(addpath))=0 then OldPath = OldPath & ";" & AddPath
wscript.echo oldpath 
REM WshEnv("Path") = OldPath

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I apologize AmazingTech.  Your script is working fine.

I realized just now that I happen to be running a copy of Symantec Software Virtualization Solution on this same machine.  The software keeps a separate copy of the registry for its own purposes and "combines" the entries from the virtual side and the physical side, thus duplicating information in my system path.

I tested everything out on several other machines and it works great.

Again, sorry about the extra work you've been doing for me!


No problem.
Steve,

Your script works great too!  And I like the addition functionality you put into it for removing extra ; at the end and double ;;.

I think I'll incorporate some of your code too in the final script.
Guys, my problem is solved.  All of you deserve and A+.

How should I fairly distribute the points?  I don't want to slight anyone.

How about:

15% to 97WideGlide for an early response and good references
40% to dragon-it for providing the append code and handling duplicate and ending semicolons
55% to AmazingTech for providing code that handles duplicate append entries and troubleshooting support

And another 1000 points for everyone for monitoring this thread all day!
u might want to dock me a few points for wrong answers...
Doesn't matter to me. But dragon-it provided the solution to your original question.
Maybe accept dragon-it as the solution and the others as assists.
Give AmazingTech the answer... I was away most of the day.  Steve

BTW Haven't tried this with users that are not admins of their machines.  might not be an issue in your environment and of course I am always an admin of any machine I use :-) but I would assume a non-admin won't be able to change the system path.  
Haha, take your pick, doesn't bother me, off to bed at last having got my VM server back up and running...
Excellent work and thanks for helping me out!