Link to home
Start Free TrialLog in
Avatar of fireburn11
fireburn11

asked on

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!
Avatar of TheGorby
TheGorby
Flag of United States of America image

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

Sorry - on line 19 change "passed" to "fail" (I had set it to "passed" for testing to make sure the email was getting sent)
Avatar of fireburn11
fireburn11

ASKER

hi TheGorby, I dont know what the autoit is... sorry.
Avatar of Chris Dent
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
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.
Hi Chris,

sure, powershell is fine. Thanks
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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