Administrative Scripting

In recent years , Windows administrators have begun to move away from batch file-based automation to more powerful and flexible administrative scripts written in ActiveX Scripting languages such as Visual Basic Scripting Edition (VBScript).

Batch files have always offered a reasonable degree of flexibility, and with the expanded command-line support in Windows Server 2003, batch files get a new lease on life. However, batch files' functionality is limited to the features provided by command-line utilities and the evaluation of relatively simple logical conditions. Scripts written in VBScript, on the other hand, offer much more flexibility.

  • For more information on Windows Server 2003's new command-line tools, see "New Command Line Tools," p. 225 .

VBScript scripts can be written using Windows Notepad, although experienced script authors prefer tools such as the free Programmer's File Editor (PFE; downloadable from www.download.com), which displays line numbers and has some features better suited for writing scripts. VBScripts can take advantage of almost any operating system feature because VBScripts can use Component Object Model (COM) objects, the basic building blocks of Windows features such as database access, file manipulation, and so forth. VBScript can also fully manipulate Windows Management Instrumentation, querying and modifying operating system and application configuration settings. For example, Listing 14.1 shows a simple script that queries a remote computer for key configuration information, which might help you remotely inventory computers on your network.

Listing 14.1 Using WMI to Query Remote Information
[View full width]
 Set System = GetObject("winmgmts:{impersonationLevel=impersonate} !//products1/root/cimv2: graphics/ccc.gif Win32 ComputerSystem=""PRODUCTS1""") WScript.Echo System.Caption WScript.Echo System.PrimaryOwnerName WScript.Echo System.Domain WScript.Echo System.SystemType 

To test this script, type it into Windows Notepad. Save the file with a vbs filename extension (be sure to enclose the entire filename in quotes; otherwise , Notepad will add a txt extension automatically). Double-click the vbs file to run the script.

Caution

Some organizations prohibit the use of scripting because it can be an easy vector for computer viruses. Your organization might have removed or disabled Wscript.exe , the executable that actually runs the scripts. Your organization also might block vbs files. If scripts are somehow disabled in your organization, be sure to consult your company's computer and security policies prior to reenabling scripting on your computer.

So, what does the script in Listing 14.1 do? The first line sets a variable named System equal to a WMI query. You'll actually need to modify this query for use in your organization, replacing the two instances of products1 with the computer name from which you want to query information. After the query executes, the System variable becomes a WMI object, with properties such as PrimaryOwnerName , SystemType , and Domain . The remaining lines of the script simply display the values of these properties by using the Wscript.Echo command, which displays the values in a pop-up message box.

Scripting can be useful without WMI, too. Listing 14.2 shows a sample VBScript that rotates the log files from IIS, saving old log files to an archive folder and deleting the oldest archived logs.

Listing 14.2 IIS Log File Rotation Script
 ' We'll take yesterday's log and move it to ' an archive folder. We'll delete the log file ' that's 30 days old from the archive ' ---------------------------------------------------------- 'declare variables Dim varLogPath, varService, varArchive, varLogFile Dim varYear, varMonth, varDay Dim objFS Dim var30Days ' ---------------------------------------------------------- ' set up variables varLogPath = "c:\winnt\system32\logfiles\" varService = "w3svc2\" varArchive = "c:\winnt\LogArchive\" ' ---------------------------------------------------------- ' get yesterday's date varYesterday = DateAdd( "d", -1, Date() ) ' ---------------------------------------------------------- ' create a formatted log file name ' for yesterday's log file ' 1. then the 2-digit year varYear = Right( DatePart( "yyyy", varYesterday), 2) ' 2. Now the month - make sure it's 2 digits! varMonth = DatePart( "m", varYesterday ) If Len(varMonth) = 1 then   varMonth = "0" & varMonth End If ' 3. Now the day - make sure it's 2 digits! varDay = DatePart( "d", varYesterday ) If Len(varDay) = 1 then   varDay = "0" & varDay End If ' 4. Complete the log file name varLogFile = "ex" & varYear & varMonth & varDay & ".log" ' ---------------------------------------------------------- ' Create a file system object Set objFS = WScript.CreateObject("Scripting.FileSystemObject") ' ---------------------------------------------------------- ' Move the file to the archive path objFS.MoveFile varLogPath & varService & varLogFile, varArchive & varLogFile ' ---------------------------------------------------------- ' get date for 30 days ago var30Days = DateAdd( "d", -30, Date() ) ' ---------------------------------------------------------- ' create a formatted log file name ' for 30-day-ago log file ' 1. then the 2-digit year varYear = Right( DatePart( "yyyy", var30Days), 2) ' 2. Now the month - make sure it's 2 digits! varMonth = DatePart( "m", var30Days ) If Len(varMonth) = 1 then   varMonth = "0" & varMonth End If ' 3. Now the day - make sure it's 2 digits! varDay = DatePart( "d", var30Days ) If Len(varDay) = 1 then   varDay = "0" & varDay End If ' 4. Complete the log file name varLogFile = "ex" & varYear & varMonth & varDay & ".log" ' ---------------------------------------------------------- ' Delete the file from the archive path objFS.DeleteFile varArchive & varLogFile 

This script performs some pretty straightforward tasks :

  • The script starts by declaring variables, which store values used by the rest of the script.

  • The script defines the file paths for the IIS logs, the particular IIS instance, and the archive folder. You'll need to change these definitions to match your environment.

  • The next several sections manipulate dates to create log filenames. Keep in mind that IIS log filenames are based on dates.

  • The script uses Windows's FileSystemObject to manipulate files and folders, moving the current log file to the archive path and then deleting the log file from 30 days ago (if one exists).

This script isn't perfect. As is, for example, it displays an error if the paths you provide aren't correct. But the script is a good example of how VBScript can make administration easier and more automated. You could, for example, use Task Scheduler to configure this script to run every morning, automatically rotating your script files.

graphics/web_icon.gif

For a quick tutorial in using VBScript for administrative purposes, visit www.samspublishing.com and enter this book's ISBN number (no hyphens or parentheses) in the Search field; then click the book's cover image to access the book details page. Click the Web Resources link in the More Information section, and locate article ID# A011403 .




Microsoft Windows Server 2003 Delta Guide
Microsoft Windows Server 2003 Delta Guide (2nd Edition)
ISBN: 0672326639
EAN: 2147483647
Year: 2005
Pages: 136

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