Section A.1. How to Write Scripts


A.1. How to Write Scripts

WSH scripts are easy to write. The following example is a very simple one in VBScript called SIMPLE.VBS.

MsgBox "Hi World!"

All you have to do is open your favorite text editor and add the command. You then save the file with a specific filename extension, VBS for VBScript and JS for Jscript. Then you can double-click the script and it will run using WSH. If you open Task Manager (press Ctrl+Shift+Esc) you will notice a wscript.exe process running, this is the WSH process that is executing the script. Figure A-1 shows the output of the script, which is a simple dialog box with a text string in it. The script uses the VBScript MsgBox function.

Figure A-1. Output from a very simple script


Now let's take a look at a slightly more complex script called SIMPLE ADSI.VBS. This script makes use of ADSI to display the description of a user.

Dim objUser 'A variable representing my user set objUser = GetObject("LDAP://cn=Betty Parsons," & _                 "ou=Pre-Sales,ou=Sales,dc=rallencorp,dc=com") MsgBox objUser.Description set objUser = Nothing

The first line is a variable declaration. We are declaring that objUser is the name for an object from which we are going to retrieve information contained within Active Directory. The Dim keyword is used to declare a variable and the apostrophe (') indicates that everything following it is a comment that will not be executed.

The second line is too long to print on the page, so we have broken it into two with an underscore (_) continuation character at the end of the line. It tells the interpreter that it should read the next line as if it were joined to the end of the first. The ampersand character (&) is used to concatenate the separated strings together. The entire line, ignoring the underscore, uses the objUser variable to hold a reference to a user object via a call to VBScript's GetObject function, passing the distinguished name of the user.

The third line simply uses the VBScript MsgBox function again to print out the description of the Betty Parsons user object. The dot signifies that Description is a property method available for the specific type of object we are accessing, which in this case is a user.

The last line simply discards the reference to Betty Parsons, and objUser becomes empty again. Strictly speaking, at the end of a script, the system discards all references anyway, but we are including it for completeness.

So, printing out properties of objects in Active Directory isn't hard at all.



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net