Shared computers are often some of the hardest-working machines in a company. Because these computers are in use all day, they're typically unavailable for preventive maintenance. Lack of maintenance can trigger a cascade of problems that grow to disaster proportions. Let's look at how to head off such disasters by automating the maintenance and monitoring of shared computers.
Suppose that some of the busiest computers in your company are the Windows 2000 Professional and Windows NT 4.0 PCs in the conference rooms. These PCs connect to projection systems so that presenters can use visual aids, such as Microsoft PowerPoint presentations. Typically, users log on to the conference-room PCs, then copy their presentations to the desktop. Having a local copy avoids possible interruptions from network glitches. In addition, graphic-intensive presentations play more smoothly when the file is local rather than on the network.
Because the conference-room PCs aren't available for regular maintenance, PC problems have prompted several calls to the Help desk. When a corporate manager attempted to copy a large PowerPoint file to one PC, he found that the hard disk was full. Another user discovered that a PC had a virus when he copied a file from that PC to his desktop computer. And an administrator found that a user with Administrator rights had given the Everyone group Full permission to the entire C drive.
Management has asked you to write a script to prevent such problems. After meeting with the Help desk team members and management, you determine that the script needs to perform certain cleanup tasks weekly and report on the PCs' health monthly.
Weekly cleanup. To prevent the conference-room PCs' hard disks from becoming full, the script needs to perform the following cleanup tasks weekly:
- Delete obsolete user profiles. Because users often forget to delete their graphic-intensive presentations, the conference-room PCs contain obsolete files saved in old user profiles. By deleting the user profiles, you also delete the presentation files. Based on management's input, you determine that the script needs to delete user profiles that have been inactive for more than 120 days.
- Delete email files. Some users have accessed their email accounts on the conference-room PCs and saved messages to local personal store (.pst) files. Using these PCs for email goes against company policy, so management has given you permission to delete any saved .pst files.
- Delete temporary files. You've found that several applications on the conference-room PCs create large .tmp files in the Temp folder and elsewhere. These .tmp files consume valuable disk space and can be deleted with no negative effect. Thus, the script needs to delete all .tmp files.
Monthly reporting. Despite the weekly cleanup, problems can still occur. To give advance notice of possible problems, you want the script to email a monthly health report for each PC to certain Help desk team members. You want each PC health report to specify the
- available drive space. Because users often copy graphic-intensive files to the conference-room PCs, drive-space utilization still is a concern even with the deletion of inactive profiles and temporary files. Thus, you determine that the script needs to report the amount of available drive space.
- date of the virus-definition file. You've found that the automatic update feature in your McAfee VirusScan 4.x software isn't working properly. As a result, some PCs have older virus-definition files, putting those PCs at risk for infections by newer viruses. The PCs' configurations differ, so you can't simply reinstall the software into the same folder on each PC. Thus, you want the script to report the date of the virus-definition file so that the Help desk knows whether the file needs to be updated.
- status of the Messenger service. On the conference-room PCs, the Messenger service has been turned off to prevent Net Send messages from popping up on screen in the middle of a presentation. Because users sometimes inadvertently turn this service back on, you want the script to report the status of this service.
- status of AT&T's Virtual Network Computing (VNC). The VNC service is installed on the conference-room PCs so that users can control their presentations remotely. (For information about VNC, go to http://www.uk.research.att.com/vnc.) You want the script to report the status of this service.
Let's take a look at how the script CleanupAndReport.bat meets these needs. Although this script was originally written to clean up and monitor conference-room PCs, you can easily adapt it to clean up and monitor other shared PCs or other crucial PCs and servers.
Deleting Obsolete User Profiles
To remove inactive user profiles, CleanupAndReport.bat uses the Del-prof utility. You can find this utility in the Microsoft Windows 2000 Server Resource Kit and the Microsoft Windows NT Server 4.0 Resource Kit. Delprof is OS-specific, so you need to use the appropriate version on each PC. If you use the wrong Delprof version, you might receive the error message Entry point not found. If you have a mixed environment, you can point the CleanupAndReport.bat script to the source files on a machine that has the same OS as the conference-room PC.
For this usage, the command to run Delprof is
delprof.exe /q /d:120
The /q switch prompts Delprof to run in quiet mode, which means that you don't have to confirm that you want to delete each user profile before the utility deletes it. The /d:120 switch specifies the maximum number of days a user profile can remain inactive. In this case, Delprof deletes user profiles that have been inactive for more than 120 days.
Deleting .pst and .tmp Files
To delete .pst and .tmp files, the script uses the For command that Listing 1 shows. In this command, the DF (df.exe) utility locates a PC's drives. (Chris Dickerson's DF utility is freeware that reports drive-space statistics. You can download the utility from http://www.tsc.com/~chrisd/ public/df.zip.) Because this utility's output includes more than just a list of the PC's drives, the For command redirects the output to the Find command. The Find command searches the output for strings containing a colon (:)—in other words, for the strings that specify the drives.
On each identified drive, the Del command deletes any file whose filename ends with the .pst or .tmp extension. The /s switch prompts the Del command to delete those files from the current directory and all subdirectories. The /q switch prompts the Del command to run in quiet mode.
Creating the Health Reports
CleanupAndReport.bat uses multiple NTFS file streams to keep track of when to email the health reports each month. If you're unfamiliar with how to use NTFS file streams, see "How to Take Advantage of the Hidden Streams in Your Scripts," November 2000.
Each file stream is named %ComputerName%-maildate.txt so that the script maintains one hidden stream for each PC. (%ComputerName% is a system-defined environment variable that specifies the name of the computer to which the user is currently logged on. For more information about system-defined environment variables, see "Shell Scripting 101, Lesson 2," http://www.winscripting solutions.com, InstantDoc ID 19840.) Each time the script runs, it reads a hidden stream that contains the date on which the script sent the last health report for that PC. If a report has been sent in the past month, the script ends. If a report hasn't been sent, the script obtains the necessary data, writes and emails the report, and writes the current date to the hidden stream for that PC.
Obtaining the data. The script uses various utilities and techniques to obtain the data for the health reports. Here's a brief overview of how the script gathers the various types of data:
- Available drive space. The script executes the DF utility. The report contains the utility's output, which includes the total space and the percentage of available space on each drive.
- Date of the virus definition file. The script obtains the last modification date for the file C:\program files\ common files\network associates\ virusscan engine\4.0.xx\clean.dat. Because this file resides in different folders on different conference-room PCs, the script chains together several If Exist commands to find the correct file location.
- Status of the Messenger and VNC services. The script uses the Srvinfo utility from the Win2K or NT resource kit. The script runs this utility with the -s switch to obtain a list of the services running on each PC. If the Messenger service is running, the script writes in the report a warning that the service is running and Net Send messages could pop up during presentations. When the Messenger service isn't running, the script reports that good news. The reverse occurs for the VNC service: The script writes a warning message if the VNC service isn't running and a good-news message when the VNC service is running.
Writing and emailing the report. The script writes the data it collects to the blat.txt file. The script uses the contents of this file as the text of an email message; fills in the email message's To, From, and Subject fields; then sends the message. The Blat utility lets you create and send email messages from a shell script. This freeware is available from http:// www.interlog.com/~tcharron/blat.html. Blat's README file contains installation instructions, syntax, and usage examples. You can find another example of Blat usage in "Real-World Scripting: Using Blat to Send Email Notification Messages," November 2000.
The script uses the Blat command that Listing 2 shows. In this command, %SystemDrive%\blat.txt specifies the file that contains the text for the email message and the -s switch specifies the email message's Subject line. (%SystemDrive% is a system-defined environment variable that specifies the system drive of the computer to which the user is currently logged on.) The -server switch specifies the SMTP server to use, the -f switch specifies the sender's email address, and the -t switch specifies the recipients' email addresses. Instead of hard-coding the SMTP mail server, sender's email address, and recipients' email addresses in the command, the script takes advantage of user-defined environment variables (%MailServer%, %Sen-der%, and %Recipients%, respectively) to make the script easier to customize and maintain. (If you're unfamiliar with user-defined environment variables, see "Shell Scripting 101, Lesson 3," http://www.winscriptingsolutions .com, InstantDoc ID 20142.)
Writing the current date. After emailing the health report to the specified Help desk members, the script writes the current date to the hidden stream for each PC. The next time the script runs, it checks this date.
Using the Script
You can find CleanupAndReport.bat in the Code Library on the Windows Scripting Solutions Web site (http:// www.winscriptingsolutions.com). Here are the steps to get the script working in your environment:
- Place the script on an NTFS volume, and give the script write permission. The script must reside on an NTFS volume because CleanupAnd- Report.bat uses hidden streams. The script needs write permission because it writes to hidden streams.
- Place the Delprof, DF, and Blat utilities in the same folder on a central server. Grant read-only permission to that folder.
- Configure the folder in which you're placing the utilities. Listing 3 shows the section of the script that contains the code you need to configure. At callout A in Listing 3, replace \\dom1pdc\tools with the path to your folder.
- Configure the SMTP mail server that will send the report. At callout B, replace mail.yourcompany.com with your SMTP mail server.
- Configure the sender's email address. At callout C, replace ted.smith @yourcompany.com with the sender's email address. This address might or might not need to be an actual user account. Check with your SMTP contact to make this determination.
- Configure the recipient list. At callout D, replace fred.smith@your company.com and email@example.com with your recipients' email addresses. Use a comma to separate multiple email addresses.
- Schedule the script to run weekly. Use an account that has read access to the utilities and write access to the script.
I've tested CleanupAndReport.bat on Win2K Pro and Win2K Advanced Server running Service Pack 2 (SP2) and NT Server 4.0 and NT Workstation running SP6. You need to thoroughly test the script on your machines before deploying the script in a production environment.