Advertisement
TimSutton

ADReplicationHealth

Jun 20th, 2016
1,707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# AD replication Summary
  2.  
  3. Description: Daily / on demand script that confirms AD replication health between all DCs and emails results.
  4.  
  5. Source: The internet.
  6.  
  7. Version Control:
  8.  
  9. 1 - Tim Sutton
  10.  - Initial implemntation.
  11.  - Minor tweaks from source to suit our environment.
  12.  
  13. #>
  14.  
  15.  
  16. Function sendEmail ([String] $body)
  17. {
  18.     $MailMessage = New-Object System.Net.Mail.MailMessage
  19.     $MailMessage.From = "DC@domain.com"
  20.     $MailMessage.To.Add("ITOps@domain.com")
  21.     $MailMessage.Subject = "AD Daily Replication Summary"
  22.     $MailMessage.Body = $body
  23.     #$MailMessage.Priority = "High"
  24.     $MailMessage.IsBodyHtml = $True
  25.  
  26.     $SMTPClient = New-Object System.Net.Mail.SMTPClient
  27.     $SMTPClient.Host = "127.0.0.1"
  28.     $SMTPClient.Send($MailMessage)
  29. }
  30.  
  31.  
  32. # Get the replication info.
  33. $myRepInfo = @(repadmin /replsum * /bysrc /bydest /sort:delta)
  34.  
  35. # Initialize our array.
  36. $cleanRepInfo = @()
  37.    # Start @ #10 because all the previous lines are junk formatting
  38.    # and strip off the last 4 lines because they are not needed.
  39.     for ($i=10; $i -lt ($myRepInfo.Count-4); $i++) {
  40.             if($myRepInfo[$i] -ne ""){
  41.             # Remove empty lines from our array.
  42.             $myRepInfo[$i] -replace '\s+', " "          
  43.             $cleanRepInfo += $myRepInfo[$i]            
  44.             }
  45.             }          
  46. $finalRepInfo = @()  
  47.             foreach ($line in $cleanRepInfo) {
  48.             $splitRepInfo = $line -split '\s+',8
  49.             if ($splitRepInfo[0] -eq "Source") { $repType = "Source" }
  50.             if ($splitRepInfo[0] -eq "Destination") { $repType = "Destination" }
  51.            
  52.             if ($splitRepInfo[1] -notmatch "DSA") {      
  53.             # Create an Object and populate it with our values.
  54.            $objRepValues = New-Object System.Object
  55.                $objRepValues | Add-Member -type NoteProperty -name DSAType -value $repType # Source or Destination DSA
  56.                $objRepValues | Add-Member -type NoteProperty -name Hostname  -value $splitRepInfo[1] # Hostname
  57.                $objRepValues | Add-Member -type NoteProperty -name Delta  -value $splitRepInfo[2] # Largest Delta
  58.                $objRepValues | Add-Member -type NoteProperty -name Fails -value $splitRepInfo[3] # Failures
  59.                #$objRepValues | Add-Member -type NoteProperty -name Slash  -value $splitRepInfo[4] # Slash char
  60.                $objRepValues | Add-Member -type NoteProperty -name Total -value $splitRepInfo[5] # Totals
  61.                $objRepValues | Add-Member -type NoteProperty -name PctError  -value $splitRepInfo[6] # % errors  
  62.                $objRepValues | Add-Member -type NoteProperty -name ErrorMsg  -value $splitRepInfo[7] # Error code
  63.          
  64.             # Add the Object as a row to our array  
  65.             $finalRepInfo += $objRepValues
  66.            
  67.             }
  68.             }
  69. $html = $finalRepInfo|ConvertTo-Html -Fragment      
  70.            
  71. $xml = [xml]$html
  72.  
  73. $attr = $xml.CreateAttribute("id")
  74. $attr.Value='diskTbl'
  75. $xml.table.Attributes.Append($attr)
  76.  
  77.  
  78. $rows=$xml.table.selectNodes('//tr')
  79. for($i=1;$i -lt $rows.count; $i++){
  80.     $value=$rows.Item($i).LastChild.'#text'
  81.     if($value -ne $null){
  82.        $attr=$xml.CreateAttribute('style')
  83.        $attr.Value='background-color: red;'
  84.        [void]$rows.Item($i).Attributes.Append($attr)
  85.     }
  86.    
  87.     else {
  88.        $value
  89.        $attr=$xml.CreateAttribute('style')
  90.        $attr.Value='background-color: #7BCE73;'
  91.        [void]$rows.Item($i).Attributes.Append($attr)
  92.     }
  93. }
  94.  
  95. #embed a CSS stylesheet in the html header
  96. $html=$xml.OuterXml|Out-String
  97. $style='<style type=text/css>#diskTbl { background-color: white; }
  98. td, th { border:1px solid black; border-collapse:collapse; }
  99. th { color:white; background-color:black;font-family:verdana;font-size:9pt; }
  100. table, tr, td { font-family:verdana;font-size:9pt;padding: 1px 5px; }, th { padding: 2px 5px; margin: 0px } table { margin-left:50px; }</style>'
  101.  
  102. #ConvertTo-Html -head $style -body $html -Title "Replication Report"|Out-File ReplicationReport.htm
  103.  
  104. $bodyHtml=ConvertTo-Html -head $style -body $html -Title "Replication Report"|Out-String
  105.  
  106.  
  107. sendEmail $bodyHtml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement