SideBar    Using AD to Retrieve Computer Names
DOWNLOAD THE CODE:
Download the Code 39763.zip

Next, I use a For Each...Next statement (aka For Each loop) to perform the bulk of the work. Before entering the loop, I enable VBScript's error-handling mechanism: the On Error Resume Next statement. This statement does exactly what it says—that is, if the script encounters a runtime error (the On Error part), the script continues to the next statement (the Resume Next part) instead of aborting. Using On Error Resume Next lets me use VBScript's Err object to check for and handle errors gracefully.

The For Each loop iterates through the arrComputers array I created earlier. For each computer listed in the input file, the script performs the following three steps:

Step 1: Determine computer availability. To determine the target computer's availability, the script uses WshShell object's Exec method to ping the computer, as callout A in Listing 1 shows. Exec returns a reference to a WshScriptExec object, which the script uses to read and capture the Ping command's output. I use the VBScript LCase function to convert Ping's output to lowercase and store the output in the variable named strPingResults.

To evaluate output in strPingResults, the script uses an If...Then...Else statement and VBScript's InStr function. If the output contains the substring reply from, Ping succeeded, and the script proceeds to the code at callout B in Listing 1. If strPingResults doesn't contain the substring reply from, Ping failed, and the script jumps to the Else clause that callout C in Listing 1 highlights. The code in the Else clause sends the [Ping failed] error message to the console.

Step 2: Connect to the computer. To connect to WMI on the target computer, the script uses VBScript's GetObject function combined with a standard WMI connection string (also called a moniker), as the code at callout B shows. If WMI connection strings look foreign to you, I encourage you to read "WMI Monikers," May 2001, http://www.winnetmag.com, InstantDoc ID 20401. GetObject returns a reference to the WMI Scripting Library's SWbemServices object, which represents an authenticated connection to WMI on a local or remote computer. The variable named objWMIService is initialized with the SWBemServices reference that GetObject returns.

Following the connection attempt, the script uses a second If...Then...Else statement and the Err object's Number property to determine whether the connection succeeded. If Err.Number is anything other than zero, the WMI connection failed and the script sends an appropriate error message to the console. If Err.Number is zero, the connection succeeded and the script proceeds to the Else clause in the second If...Then...Else statement.

Step 3: Retrieve the service. The script uses the WMI Scripting Library's SWbemServices object reference (objWMIService) to retrieve the target service. More to the point, the script uses the SWbemServices object's Get method, which accepts three optional parameters: an object path, some flags, and a named value set. In this script, only the first parameter is necessary. The object path parameter that the script passes to Get takes the form

"Class.Key='Value'"

where Class refers to the WMI class that models the target resource, Key is the class property that identifies unique instances of the managed resource, and 'Value' is the value that uniquely identifies the instance of the resource you want WMI to retrieve. For example, consider the following object path:

"Win32_Service.Name=
    'MSSQLSERVER'"

Win32_Service is the class, Name is the Key property for the Win32_Service class, and 'MSSQLSERVER' is the unique instance of Win32_Service you want Get to retrieve. The only difference between this sample object path and the object path in IdentifySQLComputers.vbs is that, in IdentifySQLComputers.vbs, the script stores the name of the target instance in the strServiceName variable. Storing the target instance in a variable makes the script easy to adapt if you want to use the script for different services.

Prev. page     1 2 [3] 4     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.