Question : Need help with writing batch files to automate "dcdiag" and "repadmin" on eight DCs

Hello Experts,

I'm by no means a programmer, and have only used batch files to do very basic things in the past. However, I'm attempting to automate maintenance procedures on eight DCs without user/administrator interaction. I would like to run "dcdiag" and "repadmin" on all eight DCs automatically and email a notification to an email distribution list once the jobs have run. Let me outline the steps:

1) I want to locate my batch files on each DC in C:\SCRIPTS.
2) I want to schedule the batch files on each DC to run once a week.
3) I want to run "dcdiag" and "repadmin". I want to output the results to two different text files respectively. The name convention I want to use for each of this files is: "NETBIOSname-dcdiag-MMDDYY.txt" and "NETBIOSname-repadmin-replsum-MMDDYY.txt".
4) I want to copy the files created in step 3 to a network location \\servername\sharename.
5) I want to send an automatic email notification to an email distribution list once the schedule task has completed. I researched and came up with BLAT as a way to do this. I tested this on a test machine, and it worked OK. If there's another way to do it without using BLAT, I would like to know.

Here's some code snippets that I've been able to put together:
-----Code to run DCDIAG and output the results-----
@ECHO OFF
DCDIAG /v /e > C:\ADDiag\NETBIOSname-dcdiag-MMDDYY.txt
BLAT NETBIOSname-dcdiag-MMDDYY.txt -to @ -s Subject -priority 1

My biggest challenge has been how to generate the file name automatically to fit my name convention. The following code snippet finds the FQDN and requires the administrator to type the host's IP address.
@ECHO OFF
ECHO.
IF [%1]==[] GOTO Syntax
ECHO.%1 | FIND "?" >NULL
IF NOT ERRORLEVEL 1 GOTO Syntax
FOR /F "tokens=2 delims= " %%A IN ('PING -a %1 -n 1 ^| FIND "[%1]"') DO ECHO.%%A
GOTO:EOF

:Syntax
ECHO.
ECHO Usage: HOSTNAME ^
ECHO.

Answer : Need help with writing batch files to automate "dcdiag" and "repadmin" on eight DCs

Wow... I'm surprised no one has tackled this by now...

You're request is relatively easy.


Whenever you need to output DCDIAG, tell it to output to %dcdiagresults% and use the following line near the top of the batch file:

SET DCDiagResults=%computername%-dcdiag-%date:~-4%%date:~4,2%%date:~7,2%.txt

Like wise, for RepAdmin, use %repadminresults% and put this line above or below the previous one:

SET RepAdminResults=%computername%-repadmin-%date:~-4%%date:~4,2%%date:~7,2%.txt

Now, I did swap the year output and include it as a 4 digit number - so had the file been run today, it would be NetBIOSName-dcdiag-20080125.txt

I swapped the year because it will ensure the files sort correctly - otherwise, starting with month first, you'll see every january file before you see the february files and I just figured you'd prefer things to be listed chronologically.


Blat is probably fine - I use gbmailer - it's a little know command line mailer I found YEARS ago.  I have a copy of it in my batch script download on my web site.  Basically the same thing as blat.

Just so you know - Windows has a number of environment variables you can reference - just type SET at a command prompt to see a list - and you can reference any of them by using % before and after - like %variable%.
Random Solutions  
 
programming4us programming4us