Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents of Guy's Scripting Ezine No 14 - Do...Loop Until
Many VBScripts need an instruction that repeats the same sequence for multiple
objects. For example, creating hundreds of computer accounts. The
Do...Loop is my secret for telling the script to keep cycling through the same
lines, until it reaches the end of the list. Perhaps you have already used
'For...Next' loops in other programming languages? In which case you will
already have a good idea of what Do...Loop can achieve.
I would like to introduce you to the family of Do ...Loop commands
Do....Loop Until and its 'brother', Do....Until
Loop
Do....Loop While and its 'sister',
Do....While Loop
The job of this family is to cycle through repetitive tasks like adding users
from rows in spreadsheets or reading through hundreds of objects in Active Directory.
This Do...Loop family operates by going through line after line of commands,
then circling back to the beginning and repeating commands for the next item.
The secret of avoiding endless loops is having an effective command to stop. To
halt the loop, you have a choice of two prepositions: Until or While. Both
'Loop Until' and 'Loop While' achieve the same goal - breaking the cycle.
It is just that their attitude is different; 'Do...Loop Until', is
adventurous, it keeps on going until stopped. On the other hand,
'Do...Loop While' is cautious, it only goes through the loop if the condition is
met.
Do...Loop Examples
Another trade secret. I do not have the perfect Do...Loop example for Windows
2003. Instead I have two VBScripts, the first is ridiculously easy, but
doesn't do anything useful. The second example is powerful and
interesting, but it is technically demanding.
Copy the VBScript below, then paste into notepad and save as a .vbs.
Double click your .vbs file and see a message with the answer to the question:
What is the sum of 1 to 7?
' VBscript Very simple Do... Loop Until
' Guy Thomas January 2004
Option Explicit
DIM inti, intx
inti = 0
DO
inti = inti + 1
intx = intx + inti
Loop Until inti = 7
WScript.Echo " Sum of 1 to " & inti & " = " & intx
Wscript.Quit
Learning Points
Note 1: inti is the counter; while intx is keeping the score.
Note 2: With each loop, the script adds another number to the sequence
1+2+3+4+5+6+7
Note 3: Beware, as with all Loop scripts, they must have a break
statement. Loop until inti = 7 is our command to break the loop.
Note 4: In the WScript.Echo statement I have added the two variables
inti and intx. My reasoning is this, if you do change Line 13: Loop Until inti = 7 to Loop Until
inti = 10, then you still get a meaningful message.
Instructions.
- Warning this script will alter the users description.
- As usual, copy and paste into notepad and save as a .vbs. You will
need a domain to get this script to work.
- You must change line 21 for the VBScript to work in your domain.
Find then alter:
ou=Worcester, dc=cp,dc=com. I also suggest that you create an OU and test user as necessary.
- Go to Active Directory Users and Computers, User, Property, then you will
see what changes your script has made.
' VBScript
' Guy Thomas January 2004
' WARNING - I am not a great one for disclaimers, but
' watch out! This script WILL CHANGE your Users Properties
' Modifying the DESCRIPTION property of Multiple Users
' Note best to choose a test OU with a test user (To begin with)
' Check the User's Property Tab : Description
Option Explicit
DIM objConnection, objCommand, objRecordSet, objUser
DIM strChange, strADsPath, strProperty
strProperty = "Description"
strChange ="Hot Shot Professional"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://ou=Worcester, dc=cp,dc=com>;" & _
"(&(objectCategory=person)(objectClass=user));" & _
"ADsPath;subtree"
Set objRecordSet = objCommand.Execute
DO
strADsPath = objRecordset.Fields("ADsPath")
Set objUser = GetObject(strADsPath)
objUser.Put strProperty, strChange
objUser.SetInfo
objRecordset.MoveNext
Loop Until objRecordset.EOF
Wscript.Echo objRecordSet.RecordCount & _
" User accounts " & strProperty & " changed to " & strChange
objConnection.Close
Wscript.quit
Learning Points
Note 1: Sorry for repeating the warning, but this script will alter
your users properties. However, I have chosen a relatively harmless property =
description.
Note 2: By introducing the variable strChange it makes it a little
easier for you to edit the actual comment; it also makes it a lot easier to
display what the script did in the message box.
Note 3: Here is my challenge to you; carefully select another User
Property, then change the strProperty. Example strProperty =
"Office"
Method use: changetype: modify
Even if you cannot get the 'unicodePwd' to work, you can at least master the
changetype: modify. Another bonus of doing this exercise is that you will learn
the strange syntax of the - (dash).
-
Assumption: You have a user
called Guy Thomas in the Worcester OU.
-
Modification: Change
DC=cp to reflect yourdomain.
-
Note well : Syntax is
crucial. Observe carefully the single -
(dash) on line 5
-
Amendment: Change the
description below, then use notepad and save to a file: for example guydes.ldp
-
dn: CN=Guy Thomas, OU=Worcester,
DC=cp, DC=com
changetype: modify
replace: description
description: Supremo
-
Table 1
-
At the command prompt type
LDIFDE -i -f
guydes.ldf
-k
-
Check the properties of Guy
Thomas in Active Directory Users and Computers
Troubleshooting LDIFDE
Note if there is no - (Dash) on the last line, this
is the error you get.
There is a syntax error in the input file
Failed on line 4. The last token starts with 'P'.
The change-modify entry is missing the terminator '-'.
An error has occurred in the program
The disadvantage of LDIFDE is that it does not 'feed off' spreadsheets, and I
find the format a bit clunky. See more about LDIFDE here.
All good scripter need a 'Looping' command in their repertoire. Here we
investigate a simple loop to cut our teeth, then a more useful example to change
attributes in Active Directory.
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.
|