Make smarter automated batch files and scripts

There are many tasks/functions that, when automated, improve systems management capabilities and reliability.

By William T. Evans

Making use of batch files and scripts in a computer network is an essential part of a network administrator's tool kit. There are many tasks/functions that, when automated, improve systems management capabilities and reliability. An automated task can be run at any time of day. Some tasks that can be automated are:

  • System drive defragmentation
  • Permission set/update on home directories
  • Housekeeping (removing temporary files from servers or home directories)
  • Time synchronization
  • Data file backup
  • Log file export or backup
  • Restarting system services
  • Checking system up time

Making automation smarter

One problem with automated tasks (whether in batch file or script format) is that there is no user interaction. You can't gather informational messages, error messages, debug, start/stop times, and other useful information without user interaction. Typically, batch processes are in-out scenarios (information comes in and other information goes out). For example:

dir e: Dir.txt

The above batch command will run dir e: and export the directory listing to a file. Using the > character enhances the command by creating a log file dir.txt with necessary information in it. However, any error messages or other messages will be lost.

A more useful command would be:

cscript //nologouptime.vbs SERVER1 > uptime.log

This batch command will check the uptime on SERVER1 using the script UPTIME.VBS (this script is very similar to Microsoft’s uptime.exe utility) and then export the results to a text file UPTIME.LOG. This could be a useful command for automating a process that checks to see how long a server has been up and running (and therefore when it requires a scheduled restart). However, this is very much a hands off process and it's not easy to manage multiple automated tasks such as these.

This simple one-line command can be made "smarter" by using a number of command line functions and tools, like the following:

REM System UPTIME Checker
REM Check to see if system uptime is greater than 24 days
cscript //nologouptime.vbs SERVER1 > uptime.log
cscript //nologouptime.vbs SERVER2 >> uptime.log
cscript //nologouptime.vbs SERVER3 >> uptime.log
REM Process files and create email
copy start.txt uptime.txt
date /t >> uptime.txt
time /t >> uptime.txt
cat space.txt >> uptime.txt
cat line.txt >> uptime.txt
cat uptime.log >> uptime.txt
cat end.txt >> uptime.txt
move /y uptime.txt body.txt
REM Email Report file to Network Administrator
blat body.txt -to, -subject "Server UPTIME Report"
REM Clean Up
del /q body.txt
del /q uptime.log

** NOTE: Click here to download a copy of the necessary files

This enhanced batch file now performs the same check for all servers in the organization with the following additions:

  • All process activity is logged
  • Batch processing start date and time is logged
  • Batch processing end time is logged
  • Easily readable log files are created
  • A copy of the result log file is emailed to the network administrator (using blat.exe)

With these enhancements, the administrator can easily manage this automated process. There's sufficient debugging (for troubleshooting purposes) and reporting (automated e-mail to the administrator). This not only improves manageability, but also the process functionality. Now the network administrator can simply check his/her e-mail to determine if any server has been up and running for more than 24 days.

Batch and script processes certainly are an important part of any network administrator’s tool kit. Simple batch commands can be built into functional processes by building in logging, reporting, and notification systems. Doing so will increase the manageability of such processes and the management of systems administration as a whole.