Advertisement
TimSutton

DCDiagHealth

Jun 20th, 2016
1,460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# DCDiag Health Report
  2.  
  3. Description: Daily / on demand script that checks AD health using DCDiag and emails out report.
  4.  
  5. Source: The internet.
  6.  
  7. Version Control:
  8.  
  9. 1 - Tim Sutton
  10.  - Initial Implmentation
  11.  - Minor tweaks from source to suit our environment.
  12.  
  13. #>
  14.  
  15.  
  16.  
  17. Function sendEmail ([String] $body)
  18. {
  19.     $MailMessage = New-Object System.Net.Mail.MailMessage
  20.     $MailMessage.From = "DC@domain.com"
  21.     $MailMessage.To.Add("ITOps@domain.com")
  22.     $MailMessage.Subject = "DCDiag Health Summary v2"
  23.     $MailMessage.Body = $body
  24.     #$MailMessage.Priority = "High"
  25.     $MailMessage.IsBodyHtml = $True
  26.  
  27.     $SMTPClient = New-Object System.Net.Mail.SMTPClient
  28.     $SMTPClient.Host = "127.0.0.1"
  29.     $SMTPClient.Send($MailMessage)
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. Function convertToVertical ([String] $testname)
  37. {
  38.  
  39. $stringlength = $testname.Length
  40.  
  41. for ($i=0; $i -lt $stringlength; $i++)
  42. {
  43.     $newname = $newname + "<BR>" + $testname.Substring($i,1)
  44. }
  45.  
  46. $newname
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. import-module ActiveDirectory
  55. $ADInfo=Get-ADDomain
  56. $allDCs=$ADInfo.ReplicaDirectoryServers
  57.  
  58. $testnamecount=0
  59.  
  60. $a = "<style>"
  61. $a = $a + "body{color:#717D7D;background-color:#F5F5F5;font-size:8pt;font-family:'trebuchet ms', helvetica, sans-serif;font-weight:normal;padding-:0px;margin:0px;overflow:auto;}"
  62. #$a = $a + "a{font-family:Tahoma;color:#717D7D;Font-Size:10pt display: block;}"
  63. $a = $a + "table,td,th {font-family:Tahoma;color:Black;Font-Size:8pt}"
  64. $a = $a + "th{font-weight:bold;background-color:#ADDFFF;}"
  65. #$a = $a + "td {background-color:#E3E4FA;text-align: center}"
  66. $a = $a + "</style>"
  67.  
  68. ##############################
  69. foreach ($item in $allDCs)
  70. {
  71.     $logfile = "C:\_Scripts\DCDiagHealth\dcdiag_$item.txt"
  72.     #Dcdiag.exe /v /s:$item >> $logfile
  73.     Dcdiag.exe /s:$item >> $logfile
  74.  
  75.     #New-Variable "AllResults$item" -force
  76.     #$c = $AllResults + $item
  77.      
  78.     $AllResults = New-Object Object
  79.     $AllResults | Add-Member -Type NoteProperty -Name "ServerName" -Value $item
  80.     #$TestCat = $Null
  81.    
  82.     $table+="<tr><td>$item</td>"
  83.     Get-Content $logfile | %{
  84.         Switch -RegEx ($_)
  85.         {
  86.              #"Running"       { $TestCat    = ($_ -Replace ".*tests on : ").Trim() }
  87.              "Starting"      { $TestName   = ($_ -Replace ".*Starting test: ").Trim() }
  88.              "passed|failed" { If ($_ -Match "passed") {
  89.              $TestStatus = "Psd"
  90.               } Else {
  91.              $TestStatus = "Fld"
  92.               } }
  93.         }
  94.  
  95.         If ($TestName -ne $Null -And $TestStatus -ne $Null)
  96.         {
  97.             $TestNameVertical = convertToVertical($TestName)
  98.  
  99.  
  100.             $AllResults | Add-Member -Name $("$TestNameVertical".Trim()) -Value $TestStatus -Type NoteProperty -force
  101.          
  102.             if($TestStatus -eq "Fld"){
  103.                 $table+="<td style=""background-color:red;"">$TestStatus</td>"
  104.             }else{
  105.                 $table+="<td style=""background-color:green;"">$TestStatus</td>"
  106.             }
  107.            
  108.             if($testnamecount -lt 29){
  109.                 $allTestNames = $allTestNames + "<BR>" + $TestName
  110.                 $testnames+="<td style=""background-color:#CCE3FF;"">$TestNameVertical</td>"
  111.                 #$testnames+="<td class=""titlestyle"">t<BR>e<BR>s<BR>t</td>"
  112.                 $testnamecount++
  113.             }
  114.                    
  115.             $TestName = $Null; $TestStatus = $Null
  116.         }
  117.         New-Variable "last$item" -force -Value $AllResults
  118.     }
  119.     $table+="</tr>"
  120.     Remove-Item $logfile
  121.    
  122. }
  123.  
  124.  
  125.  
  126.    
  127.    
  128.    
  129.    
  130.    
  131.  
  132. $html="<html><head>$a</head><table><tr><td>S<BR>e<BR>r<BR>v<BR>e<BR>r<BR>N<BR>a<BR>m<BR>e</td>" +$testnames + "</tr>" + $table + "</table><BR><BR>Tests ran: $allTestNames</html>"
  133. #$html | out-file "C:\_Scripts\DCDiagHealth\final.html"
  134. $body = $html | out-string
  135. sendEmail $body
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement