• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3061
  • Last Modified:

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?


0
curt2000
Asked:
curt2000
  • 13
  • 9
  • 6
  • +1
4 Solutions
 
97WideGlideCommented:
I'm not a VB guy but how about something like :

if (InStr(WshEnv("Path"), "M:\DB\whatever\") == 0) {
 ...
}

sorry if I'm way off base.
0
 
Steve KnightIT ConsultancyCommented:
Logical answer to me is to check the path to see if your entry is in it, i.e. something like this.  For existing entries that are dodgy could do some string handling to remove all your entries.

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

Steve
0
 
Steve KnightIT ConsultancyCommented:
Sorry, hadn't hit submit there for a while.... same sort of answer as first one...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
curt2000Author Commented:
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.
0
 
curt2000Author Commented:
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).
0
 
97WideGlideCommented:
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.
0
 
97WideGlideCommented:
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()
0
 
AmazingTechCommented:
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

0
 
curt2000Author Commented:
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.
0
 
curt2000Author Commented:
Thanks for the helpful links 97WideGlide.  I'm going to study up an try to learn something new.
0
 
AmazingTechCommented:
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) + ";", "")
0
 
curt2000Author Commented:
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.


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

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

HIH
0
 
curt2000Author Commented:
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.  :-)
0
 
AmazingTechCommented:
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

0
 
curt2000Author Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
AmazingTechCommented:
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

0
 
AmazingTechCommented:
Wait scrap the last one.

Use this one!!!
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

Open in new window

0
 
curt2000Author Commented:
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\

:-)
0
 
curt2000Author Commented:
Thanks for the help Steve.  We're just trying to get duplicates to be removed from the path now.  I'd appreciate any help!
0
 
AmazingTechCommented:
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

0
 
Steve KnightIT ConsultancyCommented:
It looks like you are nearly there... mine seemed to be having problems due to an old entry with two ;; next to each other, and also the replace entry will not remove the M:\ entry if it is the last in the string as then it won't have a ";" after it.  Try this:

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
OldPath=WshEnv("Path")
wscript.echo oldpath
Addpath="M:\DB\whatever\"
rem Remove "addpath" entry from string
OldPath=Replace(lcase(OldPath), lcase(AddPath), "")
REM Replace any double ;; entries with ;
OldPath=Replace(OldPath, ";;", ";")
REM Remove any trailing ; chars at end of path
if right(oldpath,1)=";" then oldpath=left(oldpath,len(oldpath)-1)
wscript.echo oldpath
if instr(lcase(Oldpath),lcase(addpath))=0 then OldPath = OldPath & ";" & AddPath
 
WshEnv("Path") = OldPath

Steve
0
 
curt2000Author Commented:
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!


0
 
AmazingTechCommented:
No problem.
0
 
curt2000Author Commented:
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.
0
 
curt2000Author Commented:
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!
0
 
97WideGlideCommented:
u might want to dock me a few points for wrong answers...
0
 
AmazingTechCommented:
Doesn't matter to me. But dragon-it provided the solution to your original question.
0
 
AmazingTechCommented:
Maybe accept dragon-it as the solution and the others as assists.
0
 
Steve KnightIT ConsultancyCommented:
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.  
0
 
Steve KnightIT ConsultancyCommented:
Haha, take your pick, doesn't bother me, off to bed at last having got my VM server back up and running...
0
 
curt2000Author Commented:
Excellent work and thanks for helping me out!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 13
  • 9
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now