Computer Performance, Windows 2003, Exchange 2003, Logon Scripts

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator


 

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

Web This Site

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

 

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

Please report a broken link, or an error.