I just love it when readers send me their scripts. The most satisfying scripts are those where I give a reader a little help and then they go
away and solve the problem themselves. This week I feature such a script kindly sent in by Xihui Zhang.
This week's mission is to remove an individual element from the Environmental Path. If you remember, the path statement enables you to type just the name of the executable and not worry about the
drive letter and folder. Now if there is one thing worse than no path values, it's having an invalid path statement. The real life situation that Xihui faced was to prune the path on 1000 machines, hence it was worth creating a job
VBScript, which automates the task.
To investigate the background to the script, either type PATH at the cmd prompt or else visit the System Icon, Advanced, Environmental Variables. Here is an example of the path that you may see: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program
Files\Support Tools\;C:\Program Files\Windows PowerShell\v1.0\;
I rarely issue health warnings about my scripts, partly because if the code goes wrong the worst case scenario is - nothing happens. The other reason is nobody ever reads long disclaimers. However, this week I anticipate that if the script goes wrong then it could wipe out your path statement, which could be irritating rather than terminal. Therefore I suggest you take precautions and save your path
to a text file, at the cmd
prompt type:
path > guypath.txt This will save your path into notepad.
If the following script makes a mistake and deletes your path, you could copy the text from the
saved file and paste the path into the Environmental Variables tab of the System Icon. If you have to resort to this technique then remember to remove 'Path=' from the front of the text string. My
hidden agenda is persuading you to look at the path before the script prunes one of the elements.
Pre-requisites
This script is designed to work on any Windows machine.
Nevertheless, before
your script can remove a path string, you have to give it a chance and make sure that the value for strSearchFor is in your path. Therefore, to prepare for the script, I recommend that you launch the System Icon, Advanced, Environmental Variables and search the
Computer variables (not the User Variables) for the path
statement. Now add the same value as you set in strSearchFor. I chose strSearchFor = "C:\log; ", either use my value, else amend the script if you choose a different value for
strSearchFor.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
Instructions for adding multiple groups to one user
Copy and paste the example script below
into notepad or use a VBScript editor. E.g. OnScript.
Save the file with a .vbs extension, for example: Path.vbs
Double click
Path.vbs, then check at the command prompt, type Path. Observe that your script has removed the value referenced by strSearchFor.
Updated Version 3.8
' =============================================== ' Path.VBS ' To remove strings in PATH ENVIRONMENT VARIABLE ' Authors Xihui Zhang and Guy Thomas ' Version 3.8 - June 2005 '
============================================== Option Explicit On Error Resume Next
Dim objReg, strComputer, strValueRight, strValueLeft Dim strSearchFor, strKeyPath, strEntryName, strValue
Dim strTotalLength, strSearchForLength, strStartAt dim strArraySearchFor
Const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." ' Each value in the Array must end with a semicolon ";"
strArraySearchFor=Array("c:\log;", "c:\logs;")
' WMI Section which connects to registry (StdRegProv) ' Gets value for strValue (GetExpandedStringValue) Set objReg=GetObject _ ("winmgmts:{impersonationLevel=impersonate,(Security)}!\\"
_ & strComputer & "\root\default:StdRegProv")
Function surgery ' Calculates strStartAt which cuts the strSearchFor value from the path strSearchForLength=len(strSearchFor) strStartAt = inStr(lCase(strValue), lCase(strSearchFor))
'
Builds the new Path ' Joins the Values to the left and right of strSearchFor strTotalLength=len(strValue) strValueLeft=left(strValue,(strStartAt-1)) strValueRight=right(strValue, _
(strtotalLength-strSearchForLength-(strStartAt-1))) strValue=strValueLeft + strValueRight
' Reset the Path in the registry (SetStringValue) objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
strEntryName, strValue end function
ˆ
Original Version
' =============================================== ' Path.VBS ' To remove a string in PATH ENVIRONMENT VARIABLE ' Authors Xihui Zhang and Guy Thomas '
Version 3.2 - May 2005 ' ============================================== Option Explicit Dim objReg, strComputer, strValueRight, strValueLeft Dim strSearchFor, strKeyPath, strEntryName, strValue
Dim strTotalLength, strSearchForLength, strStartAt, strAdd
Const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." ' The value in variable strSearchFor must end with a semicolon ";" strSearchFor="e:\log;"
' WMI Section which connects to registry (StdRegProv) ' Gets value for strValue (GetExpandedStringValue) Set objReg=GetObject _ ("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" _ &
strComputer & "\root\default:StdRegProv")
' -------------------------------------------------------- ' Calculates strStartAt which cuts the strSearchFor value from the path
strSearchForLength=len(strSearchFor) strStartAt = inStr(lCase(strValue), lCase(strSearchFor))
if strStartAt=0 then strValue=strValue+";" strStartAt = inStr(lCase(strValue), lCase(strSearchFor))
if strStartAt=0 then Wscript.quit(0) end if end if
' Builds the new Path ' Joins the Values to the left and right of strSearchFor strTotalLength=len(strValue) strValueLeft=left(strValue,(strStartAt-1))
strValueRight=right(strValue, _ (strtotalLength-strSearchForLength-(strStartAt-1))) strValue=strValueLeft + strValueRight
' Reset the Path in the registry (SetStringValue) objReg.SetStringValue
HKEY_LOCAL_MACHINE, strKeyPath, _ strEntryName, strValue
Wscript.Quit(1)
' End of Path VBScript
Learning Points
Note 1: ("winmgmts:{impersonationLevel=impersonate,(Security)}!\\"
_ & strComputer & "\root\default:StdRegProv"). You may have seen scripts that employ LDAP:// RootDSE to access Active Directory, well here WMI connects to the registry with StdRegProv.
Note
2: The Standard Registry Provider (StdRegProv) requires us to set Const HKEY_LOCAL_MACHINE. I tried removing this CONST value, but the script failed with a nasty error, after that I just accepted the
need for the Const statement.
Note 3: As luck would have it, Yitzchok Lavi wrote explaining that winmgmts:{impersonationLevel=impersonate works better if you add: ,(Security).
Note
4: Its well worth tracking strValue to observe how the script manipulates the value that the registry holds for Path.
Note 5: One of the star features of this script is
the way that it slices up the long path statement, removes the value specified by strSearchFor, then rejoins the two remaining pieces of the path to remake the new statement minus the offending value.
Note 6:
For me the last pieces of understanding how this script worked were examining GetStringValue and SetStringValue.
At face value this script just removes an offending value
from the path statement, for example C:\log. Yet, if you delve deeper you will discover a wealth of techniques that you can apply to other scripts, for example, StdRegProv with its corresponding CONST value.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
*
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.