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.
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: 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.
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 :
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.
|