You may recall from earlier columns that VBScript normally runs through a script from top to bottom until it runs...
out of commands. One way to change this flow is to use conditional statements that set conditions under which code should execute. If the conditions aren't met, the code doesn't run.
The simplest conditional statement is the If... Then. IF this condition is true, THEN do this. If it's Tuesday, then have lunch with Joe. A variation is If...Then...Else, which says: IF this condition is true, THEN do this, ELSE do that. If it's Tuesday, then have lunch with Joe -- otherwise, have lunch with Teresa.
If...Then statements are fine when a script must only choose from among a couple of choices, but they can get unwieldy quickly. If you've got more than a couple of potential lunch dates, you're better off with a Select Case statement.
Select Case sets the condition that you're testing (in this case, the day of the week) and then has options for each result, like this:
Select Case Day
Case Monday lunch with Sue
Case Tuesday lunch with Joe
Case Wednesday lunch with Francis
If you don't have lunch dates every day of the week, then you can take advantage of an option of Select Case: Case Else. This option says that, if it's any result other than one of the ones called out explicitly, perform this action. In this example, you'll set a variable equal to oNet.UserName and, based on the value of this variable, map the appropriate network drives to drive letters. Case Else provides a default set of mappings for any names for which you haven't explicitly defined resources.
ABOUT THE AUTHOR:
When Christa Anderson began working with Windows Server operating systems in 1992, she became increasingly interested in finding more efficient and flexible ways of performing routine tasks. Christa has written extensively about administrative scripting and taught technical sessions on the subject at conferences such as Comdex and CeBIT, helping people who had never done any scripting to write their own scripts in half a day. In addition to her interest in scripting Windows management, Christa is an authority on server-based computing and the program manager for Terminal Services licensing in Longhorn. If you have a scripting question for Christa, please e-mail her at scripting@SearchWinSystems.com.