|
Guy recommends :
View the effective permissions for a folder or shared drive. Free download try it now! |
VBScript - Do...Loop UntilIntroduction to Do...Loop Until'Best Practice' dictates that we should take steps to control errors. Another reason for error handling is that it saves time troubleshooting when you can narrow down the problem to one section of the script. In practice you have three, choices, write a statement that says ignore errors and carry on processing, allow errors to surface naturally, or add error handling statements (best), which will give you valuable information on where in the script the error is arising. Do...Loop - Introducing the FamilyI 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. However it is their attitude is that 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. Example 1 - Simple Do...Loop UntilCopy 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 Learning PointsNote 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.
Guy
Recommends: Permissions Analyzer - Free Active Directory Tool
|
||||
Custom Search
|
Guy Recommends: WMI Monitor for Powershell
|
|
Author: Guy Thomas Copyright © 1999-2013 Computer Performance LTD All rights reserved. Please report a broken link, or an error to: | |