The first half of this chapter introduced you to basic concepts underlying VBScript and gave you an idea of the kinds of tasks that can be accomplished using VBScript, particularly when the language is used in conjunction with ADSI or WMI. The second half of the chapter is a more standard reference work, focusing on VBScript methods and functions considered the most useful to system administrators. This is not an exhaustive list of all the things you can do with VBScript; if you need such a list, see the VBScript link on the Web Resources page. Instead, this section of the chapter targets a subset of VBScript functions and methods and attempts to place them in a context useful to anyone writing system administration scripts.
Statement Breaks in VBScript
Many scripting and programming languages make no attempt to match the code that is run with the actual physical lines typed into the text editor. For example, although the following Microsoft® JScript® sample covers nine physical lines of type, JScript treats it as a single code statement. This is because, for the most part, JScript does not recognize the end of a line of code until it sees the termination character (in this case, the semicolon). The actual physical lines of type taken up by the code are irrelevant.
var objWMI = new Enumerator (GetObject("winmgmts:") . InstancesOf("Win32_process")) ;
By contrast, a similar code statement written in VBScript will generate a syntax error:
Set objWMI = (GetObject("winmgmts:") . InstancesOf("Win32_process"))
This is because VBScript uses the carriage return instead of a special line termination character. To end a statement in VBScript, you do not have to type in a semicolon or other special character; you simply press ENTER.
In general, the lack of a required statement termination character simplifies script writing in VBScript. There is, however, one complication: To enhance readability, it is recommended that you limit the length of any single line of code to 80 characters. (In fact, some text editors will not allow a line to extend past 80 characters.) What happens, then, if you have a line of code that contains 100 characters?
Although it might seem like the obvious solution, you cannot split a statement into multiple lines simply by entering a carriage return. For example, the following code snippet returns a run-time error in VBScript because a statement was split by using ENTER.
strMessageToDisplay = strUserFirstName, strUserMiddleInitial, strUserLastName, strCurrentStatus Wscript.Echo strMessageToDisplay
You cannot split a statement into multiple lines in VBScript by pressing ENTER because VBScript sees a carriage return as marking the end of a statement. In the preceding example, VBScript interprets the first line as the first statement in the script. Next it interprets the second line as the second statement in the script, and the error occurs because strCurrentStatus is not a valid VBScript command.
Instead, use the underscore (_) to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line 1 is continued on line 2. To make it more apparent that line 2 is a continuation of line 1, line 2 is also indented four spaces. (This was done for the sake of readability, but you do not have to indent continued lines.)
strMessageToDisplay = strUserFirstName, strUserMiddleInitial, strUserLastName, _ strCurrentStatus Wscript.Echo strMessageToDisplay
Line continuation is more complex when you try to split a statement inside a set of quotation marks. For example, suppose you split a WMI statement using a blank space and an underscore:
Set colServiceList = GetObject("winmgmts:").ExecQuery("SELECT * FROM _ Win32_Service WHERE State = 'Stopped' AND StartMode = 'Auto' ")
If you run this script, you will encounter a run-time error because the line continuation character has been placed inside a set of quotation marks (and is therefore considered part of the string). To split this statement:
Close the first line with quotation marks, and then insert the blank space and the underscore.
Use an ampersand at the beginning of the second line. This indicates that line two is a continuation of the interrupted string in line 1.
Add quotation marks before continuing the statement.
These quotation marks indicate that this line should be included as part of the quoted string started on the previous line. Without the quotation marks, the script engine would interpret the continued line Win32_Service as a VBScript statement. Because this is not a valid VBScript statement, an error would occur.
The revised statement looks like this:
Set colServiceList = GetObject("winmgmts:").ExecQuery("SELECT * FROM " _ & "Win32_Service WHERE State = 'Stopped' AND StartMode = 'Auto' ")
When splitting statements in this fashion, be careful to insert spaces in the proper location. In the preceding sample, a blank space was added after the word "FROM" and before the closing quotation marks. If the blank space is left out, the string is interpreted incorrectly (notice how the words "FROM" and "Win32_Service" run together), and an error results:
" SELECT * FROMWin32_Service WHERE State = 'Stopped' AND StartMode = 'Auto' "