Link to home
Create AccountLog in
Active Directory

Active Directory

--

Questions

--

Followers

Top Experts

Avatar of nyceuser
nyceuser🇺🇸

AD Health Check Script
Hey all --  I need a script that will run the following two commands on my domain controller and then email the results to a group.  Any takers?

DCdiag /test:DNS /e /v
Repadmin /replsum /bysrc /bydest /sort:delta

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of RickSheikhRickSheikh🇺🇸

Care for a PowerShell script ?

Avatar of nyceusernyceuser🇺🇸

ASKER

Powershell is fine.  I made some changes.  I have a script that does a replication healthcheck.  It's called replreport.cmd.  It's a script that basically runs repladmin and outputs everthing to a txt file.  Here is the script....

@echo off

echo.
echo Gathering Report for DCLIST = %1
echo.
Echo Report for DCLIST = %1 > replreport.txt

echo. >> replreport.txt
echo. >> replreport.txt

echo Gathering Verbose Replication and Connections
echo Verbose Replication and Connections >> replreport.txt echo. >> replreport.txt
repadmin /showrepl %1 /all >> replreport.txt
echo. >> replreport.txt

echo Gathering Bridgeheads
echo Bridgeheads >> replreport.txt
echo. >> replreport.txt
repadmin /bridgeheads %1 /verbose >> replreport.txt
echo. >> replreport.txt

echo Gathering ISTG
echo ISTG >> replreport.txt
echo. >> replreport.txt
repadmin /istg %1 >> replreport.txt
echo. >> replreport.txt

echo Gathering DRS Calls
echo Outbound DRS Calls >> replreport.txt
echo. >> replreport.txt
repadmin /showoutcalls %1 >> replreport.txt
echo. >> replreport.txt

echo Gathering Queue
echo Queue >> replreport.txt
echo. >> replreport.txt
repadmin /queue %1 >> replreport.txt
echo. >> replreport.txt

echo Gathering KCC Failures
echo KCC Failures >> replreport.txt
echo. >> replreport.txt
repadmin /failcache %1 >> replreport.txt
echo. >> replreport.txt

echo Gathering Trusts
echo Trusts >> replreport.txt
echo. >> replreport.txt
repadmin /showtrust %1 >> replreport.txt
echo. >> replreport.txt

echo Gathering Replication Flags
echo Replication Flags >> replreport.txt
echo. >> replreport.txt
repadmin /bind %1 >> replreport.txt
echo. >> replreport.txt

echo Gathering Replication Summary
echo Replication Summary >> replreport.txt
repadmin /replsum /bysrc /bydest /sort:delta %1 >> replreport.txt
echo. >> replreport.txt

echo Done.
 
 I also need the DCdiag /test:dns /e /v to output to a text file.  Once the two txt files are generated, I need them to be emailed to a group daily.  Is this doable?

Avatar of RickSheikhRickSheikh🇺🇸

I don't see why not. For e.g take a look at the script below, (you will have to make changes and feed in your SMTP/Email related variables)


get-pssnapin -reg | add-pssnapin -ea 0

DCdiag /test:DNS /e /v >c:\DCDIAG.txt
Repadmin /replsum /bysrc /bydest /sort:delta >c:\ReplTest.txt

    $filename1 = "C:\DCDiag.txt"
    $filename2 = "C:\Repltest.txt"
   
    $smtpServer = "smtp.mydomain.int"

    $msg = new-object Net.Mail.MailMessage
    $att1 = new-object Net.Mail.Attachment($filename1)
    $att2 = new-object Net.Mail.Attachment($filename2)
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

    $msg.From = "itadmin@mydomain.int"
    #$msg.To.Add("itadmin@mydomain.int")
    $msg.To.Add("itadmin@mydomain.int")
    $msg.Subject = "Domain DCDIAG and Repadmin reports"
    $msg.Body = "This is the report of blah blah blah.....`r`n`r`n"
    $msg.Attachments.Add($att1)
    $msg.Attachments.Add($att2)
    $smtp.Send($msg)
    $att.dispose()
    }


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of RickSheikhRickSheikh🇺🇸

You can schedule this script as a task. See http://www.powergui.org/thread.jspa?threadID=6723

Avatar of RickSheikhRickSheikh🇺🇸

Save that script as a .ps1 of course.

Avatar of nyceusernyceuser🇺🇸

ASKER

This is awesome.  I have a question.  in the script, can I replace the "Repadmin /replsum /bysrc /bydest /sort:delta >c:\ReplTest.txt" line with the script I made (replreport.cmd)?  will it run the command script?  

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of RickSheikhRickSheikh🇺🇸

You can insert in any command you like, just make sure that you are specifying in the (filename) variables where the script should pick up and attach the files from.

Avatar of nyceusernyceuser🇺🇸

ASKER

ok..so I have the following saved as "adhealthcheck.ps1" :
get-pssnapin -reg | add-pssnapin -ea 0

DCdiag /test:DNS /e /v >c:\ADHealthCheck\DNSreport.txt
c:\ADHealthCHeck\Rplreport.cmd *

   $filename1 = "C:\ADHealthCheck\DNSreport.txt"
   $filename2 = "C:\ADHealthCheck\replreport.txt"
   
   $smtpServer = "exchange.ehm.com"

   $msg = new-object Net.Mail.MailMessage
   $att1 = new-object Net.Mail.Attachment($filename1)
   $att2 = new-object Net.Mail.Attachment($filename2)
   $smtp = new-object Net.Mail.SmtpClient($smtpServer)

   $msg.From = "ADHealthCheck@ehm.com"
   #$msg.To.Add("ADHealthCheck@ehm.com")
   $msg.To.Add("ADHealthCheck@ehm.com")
   $msg.Subject = "Active Directory Health Check Reports"
   $msg.Body = "This is a report of DNS and Replication Health Checks.....`r`n`r`n"
   $msg.Attachments.Add($att1)
   $msg.Attachments.Add($att2)
   $smtp.Send($msg)
   $att.dispose()
   }

  How do I run it in powershell?

Avatar of nyceusernyceuser🇺🇸

ASKER

I get the following error when I try to run the script


SNAG-0022.jpg

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of RickSheikhRickSheikh🇺🇸

That particular error can be solved by setting the execution policy to remoted signed.

Set-ExecutionPolicy -ExecutionPolicy remotesigned

However, I gave this script a try and it seems that the script is not given enough time to grab the txt files with results. It will need a modification.

Will take a look.

Avatar of nyceusernyceuser🇺🇸

ASKER

Thanks...i figured the execution policy out...now I get this error
SNAG-0023.jpg

Avatar of RickSheikhRickSheikh🇺🇸

Try adding a curly braces here :

   {
   $filename1 = "C:\ADHealthCheck\DNSreport.txt"
   $filename2 = "C:\ADHealthCheck\replreport.txt"
    .
    .
    .
    .

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of nyceusernyceuser🇺🇸

ASKER

Ok....my issue resides in the .cmd script i'm trying to run.  How do I add that .cmd script correctly into the poweshell script?
SNAG-0024.jpg

Avatar of RickSheikhRickSheikh🇺🇸

For the CMD, you may have to schedule that as a separate task preceding this script and just make Powershell pickup the resulted file. Do this, lets just verify the SMTP piece and have it send you an email with nothing using the script below (modify the SMTP and email settings)

$SMTPserver = "mysmtp.nowhere.now"
$fileattachment = "c:\\boot.ini"
$from = "hans@nowhere.now"
$to = "somebody@somewhere.org"
$subject = "PowerShell Test"
$emailbody = "this is my very first email send through PowerShell 1.0"

$mailer = new-object Net.Mail.SMTPclient($SMTPserver)
$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$attachment = new-object Net.Mail.Attachment($fileattachment)
$msg.attachments.add($attachment)
$mailer.send($msg)

Avatar of nyceusernyceuser🇺🇸

ASKER

Nope...never recieved the email.  Do I need an email client installed on the server that I'm running the script from?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of RickSheikhRickSheikh🇺🇸

No you don't. That is exactly why I had you try that. Your SMTP server must allow you send emails. From the last test script I posted, I verified that it works for me. So you need to work on your SMTP issues, talk to your email group, once you can receive the test email, then simply use the attachment sections to attach the both files. You will just have to schedule another task for both commands that you run preceding the email task. And to schedule the Powershell task, see the link I had attached.

Avatar of nyceusernyceuser🇺🇸

ASKER

ok...one more question...does $from= "whatever.ehm.com" email address need to be a valid email?

Avatar of nyceusernyceuser🇺🇸

ASKER

I have tried the SMTP test script on 5 servers plus my desktop and I don't get any emails.  Am I doing something wrong?  Not quite sure how to troubleshoot this.  

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of RickSheikhRickSheikh🇺🇸

For my SMTP, it does not have to be a valid email address. Check with your email team regarding the SMTP. You must be allowed to use it.

Avatar of RickSheikhRickSheikh🇺🇸

regarding the 'from' email address, for my SMTP, it does have to be valid address but it must from my @mycorpdomain.com address.

Avatar of nyceusernyceuser🇺🇸

ASKER

Ok so I checked and smtp is allowed.  When I run the script it shows the script beneath the command.  Is this normal?  p.s. - i still don't get an email


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of nyceusernyceuser🇺🇸

ASKER

heres what i get...
SNAG-0025.jpg

Avatar of RickSheikhRickSheikh🇺🇸

You should disregard the first script. Try the second. But, you must receive that test email to START with. If your SMTP is not allowing you to send email on its behalf than all these scripts are useless.

Avatar of nyceusernyceuser🇺🇸

ASKER

We are using smtp for alot of stuff that works.  insight manager, telnet, WUG, etc. and the email guys said it's working.  Is there a windows script we can try thats not powershell to see if it works?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of RickSheikhRickSheikh🇺🇸

I don't have any non-powershell script to share, but I am sure you can find tons of VB script online.

Avatar of RickSheikhRickSheikh🇺🇸

Here is one short and sweet, give it shot.

http://techtasks.com/code/viewbookcode/413

Avatar of nyceusernyceuser🇺🇸

ASKER

ok im getting smtp emails now.  we fixed it.  Should I try the original script now?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of RickSheikhRickSheikh🇺🇸

Nope. Try the second one. On the second line (under file attachment) define the path to your DCDIAG result file. Test it out.

$SMTPserver = "mysmtp.nowhere.now"
$fileattachment = "c:\\boot.ini"
$from = "hans@nowhere.now"
$to = "somebody@somewhere.org"
$subject = "PowerShell Test"
$emailbody = "this is my very first email send through PowerShell 1.0"

$mailer = new-object Net.Mail.SMTPclient($SMTPserver)
$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$attachment = new-object Net.Mail.Attachment($fileattachment)
$msg.attachments.add($attachment)
$mailer.send($msg)

Avatar of nyceusernyceuser🇺🇸

ASKER

When I  use this script  
$SMTPserver = "mysmtp.nowhere.now"
$fileattachment = "c:\\boot.ini"
$from = "hans@nowhere.now"
$to = "somebody@somewhere.org"
$subject = "PowerShell Test"
$emailbody = "this is my very first email send through PowerShell 1.0"

$mailer = new-object Net.Mail.SMTPclient($SMTPserver)
$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$attachment = new-object Net.Mail.Attachment($fileattachment)
$msg.attachments.add($attachment)
$mailer.send($msg)

I get emails.  So smtp is working.  What's the next step?

Avatar of RickSheikhRickSheikh🇺🇸

And if it works, set three scheduled tasks. One for DCDIAG, one for your Repadmin script and one for PowerShell. See the link on how to schedule a PowerShell task. Your DCDIAG and Repadmin tasks should run before your PowerShell task. In your PowerShell script define two attachments and define the paths.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of RickSheikhRickSheikh🇺🇸

$SMTPserver = "mysmtp.nowhere.now"
$fileattachment1 = "c:\\DCDIAG.txt"
$fileattachment1 = "c:\\RepAdmin.txt"
$from = "hans@nowhere.now"
$to = "somebody@somewhere.org"
$subject = "PowerShell Test"
$emailbody = "this is my very first email send through PowerShell 1.0"

$mailer = new-object Net.Mail.SMTPclient($SMTPserver)
$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$attachment = new-object Net.Mail.Attachment($fileattachment1)
$attachment = new-object Net.Mail.Attachment($fileattachment2)
$msg.attachments.add($attachment)
$mailer.send($msg)

ASKER CERTIFIED SOLUTION
Avatar of RickSheikhRickSheikh🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of nyceusernyceuser🇺🇸

ASKER

ok...almost there.  when I run the script....it's only sending the replreport.txt file...its not sending the dnsreport.txt.  any reason?


SNAG-0026.jpg

Avatar of nyceusernyceuser🇺🇸

ASKER

Excellent help

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of RickSheikhRickSheikh🇺🇸

Emailing multiple files has its own trick I suppose. Haven't tried but this is what I found, feel free to test it out.

http://www.vistax64.com/powershell/176479-emailing-multiple-files.html
Active Directory

Active Directory

--

Questions

--

Followers

Top Experts

Active Directory (AD) is a Microsoft brand for identity-related capabilities. In the on-premises world, Windows Server AD provides a set of identity capabilities and services, and is hugely popular (88% of Fortune 1000 and 95% of enterprises use AD). This topic includes all things Active Directory including DNS, Group Policy, DFS, troubleshooting, ADFS, and all other topics under the Microsoft AD and identity umbrella.