vbscript to run dcdiag and only send error/failed event to me

Hi All,

I am in need of a vbscript to run dcdiag, look into the result and if there is any error/failed event, send the result to me. if no error/failed found, dont send.

Thanks in advance for your help!
fireburn11Asked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:
Here's mine, obviously we can adjust things (such as trimming out the successful tests) if you need it.

Chris
$HtmlHead = "
<title>DCDiag Results</title>
<style type='text/css'>
    body {
      font-size: 10pt;
      font-family: sans-serif;
      padding: 0px; 
      margin: 0px; 
      overflow: auto;
    }

    table        { width: 100%; border-collapse: collapse }
    a            { color: #717D7D; Display: block; }
    a.top        { color: #717D7D; Display: block; font-size: 8pt; }
    td.left      { background-color: white; width: 100px; text-align: left; }
    td.right     { background-color: white; width: 50px; text-align: right; }
    td, th       { color: black; padding: 2px; }
    th           { background-color: #C7C7C7; text-align: left }
    td           { background-color: #F7F7F7; text-align: left }
</style>"

$DCs = [ADSI]"LDAP://OU=Domain Controllers,$(([ADSI]'LDAP://RootDSE').Get('defaultNamingContext'))"
$Searcher = New-Object DirectoryServices.DirectorySearcher($DCs, "(objectCategory=computer)")
$HtmlBody = $Searcher.FindAll() | ForEach-Object {

  $ServerName = $_.Properties['name'][0]

  "<h1>$ServerName</h1>"

  dcdiag /s:$($ServerName) | ForEach-Object {

    Switch -RegEx ($_) {
      "Starting"       { $TestName = $_ -Replace '^.*:\s' }
      "passed|failed"  { If ($_ -Match 'passed') { $TestResult = "Success" } Else { $TestResult = "Failure" } }
      "Running"        { 
        $TestCategory = $_ -Replace '\s*Running\s'
        $TestCategory = $TestCategory -Replace 'tests\son\s:', '-'
        $TestCategory = [Globalization.CultureInfo]::CurrentCulture.TextInfo.ToTitleCase($TestCategory)
      }
    }

    If ($TestName -And $TestResult) {
      "" | Select-Object @{n='Test';e={ If ($TestCategory) { "$TestCategory - $TestName" } Else { $TestName } }},
        @{n='Result';e={ $TestResult }}

      $TestName = $Null; $TestResult = $Null
    }
  } | ConvertTo-Html -Fragment

  $TestCategory = $Null
}

$HtmlBody = $HtmlBody -Replace 'Failure', "<font color='red'>Failure</font>"
[String]$HtmlDocument = ConvertTo-Html -Head $HtmlHead -Body $HtmlBody

If ($HtmlBody -Match 'Failure') {
  Send-MailMessage -To "me@domain.com" -From "admin@domain.com" -Subject "DCDiag" `
    -Body $HtmlDocument -BodyAsHtml -SmtpServer "mail.domain.com"
}

Open in new window

0
 
TheGorbyCommented:
Are you familiar with AutoIT? I'm not too good with VBScript so if you didn't want to wait around for someone to post an answer in VBS, you can use the AutoIT script below. It will run DCDIAG and if the output contains the words 'error' or 'fail', it will send you an email containing the entire DCDIAG output.

AutoIT: http://www.autoitscript.com/site/autoit/downloads/
#include <INet.au3>
#NoTrayIcon

Dim $tmpoutput="C:\test.txt"
Dim $smtpsvr="192.168.1.2"
Dim $fromname="DCDiag Checker"
Dim $from="whoever@domain.com"
Dim $to="fireburn11@domain.com"
Dim $subj="DCDiag - Error or failure found"

RunWait(@ComSpec & " /c dcdiag>" & $tmpoutput)

$hnd=FileOpen($tmpoutput)
$data=FileRead($hnd)
FileClose($hnd)
Select
	Case StringInStr($data,"error",2)<>0
		_SendMail($tmpoutput)
	Case StringInStr($data,"passed",2)<>0
		_SendMail($tmpoutput)
EndSelect
FileDelete($tmpoutput)

Func _SendMail($file)
	Local $arr[100]
	$hd=FileOpen($file,1)
	FileWriteLine($hd,"STOP")
	FileFlush($hd)
	FileSetPos($hd,0,0)
	$arr[0]="An error or failure was found, below is the DCDIAG output."
	$arr[1]="--------------------------------------------------------------------------------------"
	$i=2
	While 1
		$line=FileReadLine($hd)
		If $line="STOP" Then ExitLoop
		$arr[$i]=$line
		$i=$i+1
	WEnd
	FileClose($hd)
	_INetSmtpMail($smtpsvr,$fromname,$from,$to,$subj,$arr,@ComputerName,-1)
EndFunc

Open in new window

0
 
TheGorbyCommented:
Sorry - on line 19 change "passed" to "fail" (I had set it to "passed" for testing to make sure the email was getting sent)
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
fireburn11Author Commented:
hi TheGorby, I dont know what the autoit is... sorry.
0
 
Chris DentPowerShell DeveloperCommented:
I have a script to do this, but like everyone else I use something other than VbScript to do it. Mine is PowerShell, any use?

Chris
0
 
TheGorbyCommented:
If you're interested in AutoIT at all, it's basically a simplified programming language that comes with its own compiler to make an executable. What you would do is download AutoIT and install it on any computer. Copy and paste the script I posted into the Scite script editor that comes with AutoIT, and then go to the Tools menu to compile the script into an EXE. You'd then have an EXE that can be run from any Windows computer regardless of whether AutoIT is installed on it or not.
0
 
fireburn11Author Commented:
Hi Chris,

sure, powershell is fine. Thanks
0
 
Leon FesterSenior Solutions ArchitectCommented:
I've created a simple .bat file to do my dcdiags for me.
Output is sent to a file and then open notepad with the list of tests that failed.

Not as fancy as the rest of the work done here, but gives me the same results.
Just stack the commands with the other names of the servers.
@echo off
echo "************************************" 	>dcoutput.txt
echo "Active Directory Daily DCdiag checks"   	>>dcoutput.txt
date /T >>dcoutput.txt			>>dcoutput.txt
time /T >>dcoutput.txt			>>dcoutput.txt
echo "************************************" 	>>dcoutput.txt
dcdiag /s:DCNAME | find /I "failed" >>dcoutput.txt


copy c:\code\bat\dcoutput.txt c:\code\bat\results\dcdiag-%date:~3,2%-%date:~0,2%-%date:~6,4%.txt

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.