Computer Performance, Windows 2003, Exchange 2003, Logon Scripts

 

VBScript - Option Explicit

Introduction to Option Explicit

The simple statement - Option Explicit - tightens up your scripts.  What Option Explicit says is this: if a variable is not declared, then you cannot use it.  The use of Option Explicit is twofold, to make sure that you declare variables, and to troubleshoot by generating errors to highlight mistakes if variable names are mistyped later in the script.

Declaring variables with DIM

Good scripts are planned.  Where better to start than with the variables that you will be using.  So at the beginning of the script include a DIM (Dimension) statements about the variables.  Technically this when memory is reserved for the variables.

Example 1

Here is a VBScript incorporating the Option Explicit before the Dim statements.

 

'Script to create ten computers in the Droitwich OU
Option Explicit
Dim objLeaf, objContainer, objRootDSE
Dim intCompNum, strXPStation

Set objRootDSE = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://OU=Droitwich," & _
objRootDSE.Get("defaultNamingContext"))

For intCompNum = 1 To 10
Set objLeaf = objContainer.Create("Computer", "cn=XPStation" & intCompNum)
objLeaf.Put "sAMAccountName", "strWXStation" & intCompNum
objLeaf.SetInfo
Next
WScript.Echo "10 XP Machines created in Droitwich."

 

Note 1:  Option Explicit is the first line of the VBScript (apart from the ' Remarks).  My point is that you cannot introduce Option Explicit after the DIM statements.

 

Note 2:  Experiment by deleting one of the variables e.g. objLeaf and see what error messages you get.

 

Note 3: The above example uses the Hungarian Variables - See more on Option Explicit here

 


See Also

 

 .


Google

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Let GFI EventsManager do the dirty work! Have event logs monitored automatically and get warned about critical events! Download a copy here

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.