Tip

Create a script to check integrity of your server's drives

So far in this series on detecting the integrity of your hard drive, I've shown you how to perform an automated chkdsk and dump the results to a report file. There's still one problem: Each server you run the script on ends up containing its own copy of the log file.

One way to resolve this problem is to adjust the script's output path, by directing the log files to a folder on a network drive. The script could also be adjusted to include the server's name as part of the file name. This way, you'd know which server generated each report.

This technique has a downside: You have to remember to review the logs periodically. It's better to configure the script so it emails the report to you.

Although Microsoft has released a few obscure utilities that let you send an email message from a command prompt, third-party utilities make the process easier. A company called Febooti makes a utility called

    Requires Free Membership to View

Command Line Email that lets you send email messages from a command prompt. Licenses for this product cost less than $45, but I've seen them offered for $25 on some Web sites.

The basic syntax for this utility is:

 FEBOOTIMAIL –FROM sender's e-mail address – TO recipient's e-mail address –SUBJECT message subject – TEXT message body –ATTACH filename

The command syntax is fairly straightforward. But before you add the FEBOOTIMAIL command to your script, I recommend creating an email address for each of your servers. This way, when the messages arrive in your inbox, there'll be no doubt as to which server sent it. In this article, I'll use Server1@contoso.com as the sender's address, and admin@contoso.com as the recipient's address.

Now let's look at how we can integrate the FEBOOTIMAIL command into our script. As you might recall, our batch file currently looks like this:

for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log
Chkdsk C: >c:\logs\ %datefile%

Assuming we want to email a report from a server named Server1, we could append the following command to the file:

 FEBOOTIMAIL –FROM server1@contoso.com – TO admin@contoso.com –SUBJECT Drive Integrity Report – TEXT Here is today's drive integrity report for Server1 –ATTACH c:\logs\%datafile%

This command takes the report that was just created, attaches it to an email message and sends it to the administrator.

In the real world, you might encounter other issues with this technique, such as your mail server requiring authentication. I can't give you the specifics for this technique, because the necessary authentication commands vary from one organization to the next. I recommend attempting the command without authentication first. If that doesn't work, here's Febootimail's full command syntax.

Another issue relates to scanning multiple hard drives. Almost every server has more than one drive, yet our command only scans the server's C: drive. But this is easy to remedy -- simply add another chkdsk line for each drive letter you want to scan. For example, if you wanted to scan the C: and the D: drives, your script would look like this:

 for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log Chkdsk C: >c:\logs\ %datefile% Chkdsk D: >>c:\logs\ %datefile% FEBOOTIMAIL –FROM server1@contoso.com – TO admin@contoso.com –SUBJECT Drive Integrity Report – TEXT Here is today's drive integrity report for Server1 –ATTACH c:\logs\%datafile%

Note: In the second chkdsk command, I've changed the drive letter to D: and used two > (greater than) signs to redirect the command's output. A single > sign tells the script to create a file; two of them tell the script to append the output to a file that's already been created.

One problem with including multiple scans into a single log file is that chkdsk does not report which drive it is scanning. I recommend inserting some comments into the command that will tell you which drive each section of the file pertains to.

To do this, use the ECHO command, which simply repeats whatever text follows it. If you were to type ECHO HELLO, the output would be HELLO. However, output from an ECHO command can be redirected to a file, and this provides a good way to insert notations into your log file. For example, if I wanted to flag the beginning of the D: drive scan, I could insert the following lines into the script:

 ECHO . >>c:\logs\ %datefile% ECHO ………………………..CHKDSK D:…………………………… >>c:\logs\ %datefile% ECHO. >>c:\logs\ %datefile%

Using the ECHO command to insert a blank line is difficult, because it involves inserting a null character. This is hard to show here, because the null character is invisible. So instead, I simply use a period in the first and last line, so that ECHO will insert an almost blank line into the file.

The complete script for reporting the integrity of your server's drive and emailing the report to you looks like this:

 for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log Chkdsk C: >c:\logs\ %datefile% ECHO . >>c:\logs\ %datefile% ECHO …………………………..CHKDSK D:…………………………… >>c:\logs\ %datefile% ECHO. >>c:\logs\ %datefile% Chkdsk D: >>c:\logs\ %datefile% FEBOOTIMAIL –FROM server1@contoso.com – TO admin@contoso.com –SUBJECT Drive Integrity Report – TEXT Here is today's drive integrity report for Server1 –ATTACH c:\logs\%datafile%

About the author: Brien M. Posey, MCSE, is a Microsoft Most Valuable Professional for his work with Windows 2000 Server, Exchange Server and IIS. He has served as CIO for a nationwide chain of hospitals and was once in charge of IT security for Fort Knox. He writes regularly for SearchWinComputing.com and other TechTarget sites.

More information on this topic:


This was first published in August 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.