Guy's Scripting Ezine 28 - inStr (In String)
Contents for Guy's Scripting Ezine 28 - inStr (In String)
There have been many times when I wanted a script to filter objects, for
example, suppose you want a list of all users whose name contains Beth - Elizabeth,
Bethany, Elsbeth and other derivatives.
I have to confess that in the past I have used the 'Left' function,
whereas
I would have been much better off using the 'inStr' function.
Once you realize that inStr is not some old variable that
Guy invented, but a proper scripting function, then it is easy to understand
what inStr does. inStr literally mean within the string, in a nutshell,
inStr is for filtering or selecting parts of a string.
Powerful, awkward, tricky and versatile are all words to describe inStr.
My contributions to this inStr debate are, 'know the rules' and try lots of examples.
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
The rules for the function inStr
inStr has the following four components.
InStr([start character, ] string searched, string to look for [, Binary or
Textual)
1) The first parameter is optional and says 'which character should I
start at?' The default is begin with the left most character.
2) The parameter says, 'where do I look?' Have you declared a
variable? or are you giving me a string value?
3) What should I look for? 'Bet' or 'Beth'?
4) ) Lastly another optional component. This says 'is the search at the binary level or is it textual?', in practical terms this means is it case sensitive? In part 3) above, do you mean strictly
'Beth' or do you also want beth'? A value of 1 indicates textual, in plain English this means - not case sensitive.
My strategy with inStr is to build up gradually. While this script is of little practical use, it does have interesting learning points and will set the scene for Example 2. What I am
expecting is an output of Incidentally I
wanted a script which would work on any machine.
Instructions
- Copy and paste the script below into notepad. Alternatively, use a
script editor like OnScript.
- Save the file with .vbs extension e.g. GuyFunction.vbs
- Double click and observe the message box
- If you get errors, pay attention to syntax and object names.
' GuyFunction.vbs - In string function ' VBScript to return a value of 6 for an inStr ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.3 - June 2005 '
------------------------------------------------------ Option Explicit Dim GuyFunction, strSearched, strLook
strSearched = "Elizabeth" strLook = "Beth"
GuyFunction = InStr(1,strSearched,
strLook,1) Wscript.Echo "inStr output value = "& GuyFunction Wscript.Quit
' End of example VBScript
Note 1: I expect you to receive a message box saying: inStr output value = 6. The reason that the value is 6 is because 'Beth' starts at the 6th letter of Elizabeth. Remember that
Example 1 is just a preliminary to prepare for the main Example 2. Note 2: GuyFunction = InStr(1,strSearch, strLook,1)
a) The first 1 means start at the first character.
b) The second 1 mean make a textual search. To me, 'textual' means case insensitive. Try changing to zero and see what happens. inStr 0 means that it did not find any occurrence of 'Beth'. Try changing
'Beth', to 'beth' and then running the script again.
Note 3: strSearched is the string to be checked (Elizabeth), while strLook is
tells it which letters to look for in strSearched (Beth).
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
What I like best is the way NPM suggests solutions to network
problems. Its
also has the ability to monitor the health of individual VMware
virtual machines. If you are interested in troubleshooting, and creating
network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds' Network Performance Monitor
In this second example we put inStr to some real work. But firstly,
it is vital that you create an OU with three users in your Active Directory. As you
create these objects, so you may get
ideas for objects that you wish to filter. For example to move computers
whose name contains certain letters.
Preparation:
1) Go to your Active Directory users and computers. Create an OU called
Worcester.
2) Create three users called Bethany, Joan and Elizabeth.
Instructions for InStr VBScript
- Follow the preparation above, create the Worcester OU, and the three users in Active Directory.
- Copy and paste the script below into notepad. Alternatively, use a
script editor like VBsEdit.
- Save the file with .vbs extension e.g. inStr.vbs
- Double click and observe the message box
- If you get errors, pay attention to syntax and line numbers.
- I anticipate that you will get two users, Elizabeth and Bethany
' inStr.vbs - In string function ' Example VBScript to test strings for content ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 3.4 - June 2005 '
-------------------------------------------------------' Option Explicit Dim objRootDSE, objDomain, objUser Dim strDNSDomain, strOU, strName, strPlace, strProperty
Set objRootDSE =
GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("DefaultNamingContext") strOU = "OU=Worcester," strName = "Beth" strPlace = strOU & strDNSDomain
Set objDomain = GetObject("LDAP://"
& strPlace) For each objUser in objDomain strProperty=objUser.get("name") If InStr (1,strProperty, strName, 1) > 0 Then Wscript.Echo objUser.name End If Next
WScript.Quit ' End of
example inStr VBScript
Expected result: Bethany and Elizabeth. Learning Points
Note 1: Being a function, inStr returns a value. In this example, if the user name contains 'beth' anywhere in the string, then the user's name will be echoed. From the first example you learnt that
inStr produces values. In this instance, the 'If' test check to see if the value returned is greater than zero
Note 2: If InStr (1, strProperty, strName, 1) > 0 Then
a) In the above line, the first 1 means start at the first character.
b) In the above line the second 1 means make a textual search, that is, a non case sensitive comparison.
Challenges for InStr
This week, instead of deliberate mistakes, I have a series of challenges for
you. A few
tiny changes make all the difference between the script 'working' or producing
unexpected results.
1) If InStr (1, strProperty, strName, 1) > 0 Then
Try changing this second 1 to zero and see the difference. (Now you only get
Bethany and not Elizabeth).
2) Try changing If InStr (1, strProperty, strName, 1) = 1 Then
Again you only get Bethany, but the reason is because 'Beth' is at the
beginning of the word. In fact it begins at the 1st letter hence =1.
3) Try changing this: If InStr (1, strProperty, strName, 1) = 6,
and see if you only get Elizabeth. The reason for only getting ElizaBETh,
and not Bethany that the .....BETh starts at the 6th character. The Beth in Bethany starts at the
first character.
Summary of InStr
inStr is a very useful filter for finding objects in Active Directory.
Note that the inStr example has four parameters, which control the start point, string to search, value to compare and whether it
should be case sensitive. What Next: New Example of InStr
See more about VBScript syntax
• VBScripts • WMI • Logon Scripts • Ezine 18 Carriage returns • Ezine 28 InStr
• Ezine 26 MsgBox • Ezine
46 MsgBox • Ezine 47 .put •
Ezine 59 CONST •
Ezine 61 Objects methods
• Ezine 41 VBS Select case •
Ezine 56 VBS Select case • Ezine 80 Comspec •
Free IP Tracker • Ezines
|