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. <I got this one..>
2) I want to schedule the batch files on each DC to run once a week. <I got this one...>
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". <this is my problem!!...>
4) I want to copy the files created in step 3 to a network location \\servername\sharename. <I got this one..>
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. <I'll say that I have this one unless someone has a better way of doing it...>

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 <username>@<mydomain> -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 ^<IP_address^>
ECHO.
LVL 2
bnditAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
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%.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bnditAuthor Commented:
Hi Leew,

Thanks for the reply....you're right, it turned out to be really simple. However, I ran into a snag. When I use your suggestion of using the variable to store the file name, the batch file goes into a loop. However, when I hard code the name of the file, the batch file works correctly. Do you have any ideas as to why this might be happening?
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
IF you can post the entire batch file, I can review it.  Though as I think about it, I usually get caught on that too... I think it's a problem of reassigning the variable within the for loop - so don't use the variables, just use the =... parts.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

bnditAuthor Commented:
It definitely sounds like something to do with the loop....here's the code

@ECHO OFF
SET filename=%computername%-dcdiag-%date:~-4%%date:~4,2%%date:~7,2%.txt
dcdiag /v /e > C:\ADDiag\DCDiag\%filename%
copy C:\ADDiag\DCDiag\%filename% \\<servername>\DCDiag$
del C:\SCRIPTS\body.txt < C:\SCRIPTS\y.dat
ECHO Date:%date% > C:\SCRIPTS\body.txt
ECHO From: <username>@<mydomain> >> C:\SCRIPTS\body.txt
ECHO Subject: DCDiag Test Performed on %computername% at %time% on %date% >> C:\SCRIPTS\body.txt
ECHO. >> C:\SCRIPTS\body.txt
ECHO. >> C:\SCRIPTS\body.txt
ECHO Log location: \\<servername>\%filename%
del C:\SCRIPTS\subject.txt < C:\SCRIPTS\y.dat
ECHO DCDiag Test Performed on %computername% at %time% on %date% > C:\SCRIPTS\subject.txt
BLAT C:\SCRIPTS\body.txt -to <username>@<mydomain> -sf C:\SCRIPTS\subject.txt -priority 1 -q

0
bnditAuthor Commented:
Hmmm....interesting, I was doing some testing, and I moved all of my files to the root C:\ without making any changes to my batch file and ran it again. The result? It works perfectly...I'm at a loss here.
0
bnditAuthor Commented:
Nevermind...false alarm. It did work once, but on a second try it went on a loop again.
0
bnditAuthor Commented:
Ok, so after troubleshooting and researching the looping issue with my batch file I found the answer. My batch file was running "dcdiag /v /e" on one of my DCs, and it seemed to work but after a few tries it would enter an endless loop where I would see the command line scroll through all of the lines in the batch file. The problem was that when I first created the batch file, I named it "test.bat"; this worked perfectly. I didn't realized that I was then renaming the batch file to "dcdiag.bat". This was causing my batch file to enter into a loop since the batch file shared the command's name. I renamed my batch file to "dcdiag-test.bat" and no problem.

Thank you to leew for helping me. I'm trying to reward you with 175pts so as soon as I know how to do that I will give you the points.

Here's my final code (I'm sure it's not the prettiest, but I'm not a coder by any means)

@ECHO OFF
SET filename=%computername%-dcdiag-%date:~-4%%date:~4,2%%date:~7,2%.txt
dcdiag /v /e > C:\ADDiag\DCDiag\%filename%

COPY C:\ADDiag\DCDiag\%filename% \\corp-fs01\DCDiag$

DEL C:\scripts\subject.txt
DEL C:\scripts\body.txt

ECHO Test performed on %computername% at %time% :  dcdiag /v /e >> C:\scripts\body.txt
ECHO. >> C:\scripts\body.txt
ECHO. >> C:\scripts\body.txt
ECHO ------------------------------------ NOTES ----------------------------------------- >> C:\scripts\body.txt
ECHO The DCDiag results have been saved to the following locations: >> C:\scripts\body.txt
ECHO. >> C:\scripts\body.txt
ECHO 1) Locally on %computername%:  C:\ADDiag\DCDiag >> C:\scripts\body.txt
ECHO 2) Network location:  "\\<servername>\<sharename> >> C:\scripts\body.txt

ECHO DCDiag Test Performed on %computername% at %time% on %date% > C:\scripts\subject.txt

BLAT C:\scripts\body.txt -to <username>@<mydomain> -sf C:\scripts\subject.txt -attacht C:\ADDiag\DCDiag\%filename% -priority 1 -q
0
bnditAuthor Commented:
Thank you leew for your help.
0
montaguCommented:
Bndit,

I am trying to do the same would it be possible to see the code that you managed to get it working with?

Many Thanks in advance

Regards Paul

0
bnditAuthor Commented:
Hi Paul,

dcdiag-test.txt
0
bnditAuthor Commented:
Oops, sorry...too quick on the 'submit' button. I'm attaching the two .bat files that do the work for the script (notice that you need to change the extensions to .bat). I've also gone into these files and commented out some information and replaced it with brackets < >; change this info for your environment. You'll need to use the BLAT file as that's what I use to send the email. I'm sure that now there are other alternatives to accomplish the same thing using Windows-native tools such as PowerShell; I just havent looked into it. If I do, I'll you know  =)

Well, hope this information is helpful to you.
dcdiag-test.txt
repl-test.txt
body.txt
subject.txt
0
montaguCommented:
You legend.. Thanks for that
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.