Skip to content

Instantly share code, notes, and snippets.

@Hexalon
Created May 4, 2017 02:57
Show Gist options
  • Save Hexalon/f58fb6465de46e15ba4756b00296214e to your computer and use it in GitHub Desktop.
Save Hexalon/f58fb6465de46e15ba4756b00296214e to your computer and use it in GitHub Desktop.
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.118
Created on: 2/3/2016 9:50
Created by: Colin Squier <Hexalon@gmail.com
Filename: ADReplicationReports.ps1
===========================================================================
.DESCRIPTION
Automates several AD replication diagnostic commands and email reports.
Executes:
repadmin /showrepl
repadmin /showvector dc=<domainname>,dc=<tld>
repadmin /showreps /verbose
repadmin /replsummary <DC Name>
#>
[CmdletBinding()]
param ()
function Send-Email
{
param ($Subject,
$Body)
$To = "ADAdmin@<domain>.<tld>"
$From = "ADRep@<domain>.<tld>"
$SmtpServer = "smtp.<domain>.<tld>"
$Port = 587
$Password = "password"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential `
-ArgumentList $From, $SecurePassword
Send-MailMessage -To $To -From $From -Subject $Subject -Body $Body `
-SmtpServer $SmtpServer -Credential $Credentials `
-DeliveryNotificationOption Delay, OnFailure `
-Port $Port -Attachments $File -UseSsl
}
$Args = "/showrepl /csv"
$ReportType = "showrepl"
Write-Verbose -Message "Executing 'repadmin.exe $Args'"
$Repl = New-Object System.Diagnostics.ProcessStartInfo
$Repl.FileName = "repadmin.exe"
$Repl.RedirectStandardError = $true
$Repl.RedirectStandardOutput = $true
$Repl.UseShellExecute = $false
$Repl.Arguments = $Args
$RepProcess = New-Object System.Diagnostics.Process
$RepProcess.StartInfo = $Repl
$RepProcess.Start() | Out-Null
$RepProcess.WaitForExit()
$Output = $RepProcess.StandardOutput.ReadToEnd()
$Error = $RepProcess.StandardError.ReadToEnd()
$Date = ((Get-Date).ToString('MM-dd-yy'))
$Subject = "AD $ReportType report for $Date"
$Body = "Showrepl: `n $Output `n Errors: $Error"
Write-Verbose -Message "Preparing report for $ReportType"
$ReportFile = "ADReplicationReport-$ReportType-$Date.txt"
$File = (Join-Path -Path "$env:USERPROFILE\Documents\ADReplicationReports" -ChildPath "$ReportFile")
$Output | Out-File -FilePath $File -Encoding UTF8
"Errors: $Error" | Out-File -FilePath $File -Encoding UTF8 -Append
Write-Verbose -Message "Sending $ReportType report"
Send-Email -Subject $Subject -Body $Body
$Args = "/showvector dc=ad,dc=<domainname>,dc=<tld>"
$ReportType = "showvector"
Write-Verbose -Message "Executing 'repadmin.exe $Args'"
$Vector = New-Object System.Diagnostics.ProcessStartInfo
$Vector.FileName = "repadmin.exe"
$Vector.RedirectStandardError = $true
$Vector.RedirectStandardOutput = $true
$Vector.UseShellExecute = $false
$Vector.Arguments = $Args
$VecProcess = New-Object System.Diagnostics.Process
$VecProcess.StartInfo = $Vector
$VecProcess.Start() | Out-Null
$VecProcess.WaitForExit()
$Output = $VecProcess.StandardOutput.ReadToEnd()
$Error = $VecProcess.StandardError.ReadToEnd()
$Date = ((Get-Date).ToString('MM-dd-yy'))
$Subject = "AD $ReportType report for $Date"
$Body = "Showvector: `n $Output `n Errors: $Error"
Write-Verbose -Message "Preparing report for $ReportType"
$ReportFile = "ADReplicationReport-$ReportType-$Date.txt"
$File = (Join-Path -Path "$env:USERPROFILE\Documents\ADReplicationReports" -ChildPath "$ReportFile")
$Output | Out-File -FilePath $File -Encoding UTF8
"Errors: $Error" | Out-File -FilePath $File -Encoding UTF8 -Append
Write-Verbose -Message "Sending $ReportType report"
Send-Email -Subject $Subject -Body $Body
$Args = "/showreps /verbose"
$ReportType = "showreps verbose"
Write-Verbose -Message "Executing 'repadmin.exe $Args'"
$Reps = New-Object System.Diagnostics.ProcessStartInfo
$Reps.FileName = "repadmin.exe"
$Reps.RedirectStandardError = $true
$Reps.RedirectStandardOutput = $true
$Reps.UseShellExecute = $false
$Reps.Arguments = $Args
$RepVerboseProcess = New-Object System.Diagnostics.Process
$RepVerboseProcess.StartInfo = $Reps
$RepVerboseProcess.Start() | Out-Null
$RepVerboseProcess.WaitForExit()
$Output = $RepVerboseProcess.StandardOutput.ReadToEnd()
$Error = $RepVerboseProcess.StandardError.ReadToEnd()
$Date = ((Get-Date).ToString('MM-dd-yy'))
$Subject = "AD $ReportType report for $Date"
$Body = "Showvector: `n $Output `n Errors: $Error"
Write-Verbose -Message "Preparing report for $ReportType"
$ReportFile = "ADReplicationReport-$ReportType-$Date.txt"
$File = (Join-Path -Path "$env:USERPROFILE\Documents\ADReplicationReports" -ChildPath "$ReportFile")
$Output | Out-File -FilePath $File -Encoding UTF8
"Errors: $Error" | Out-File -FilePath $File -Encoding UTF8 -Append
Write-Verbose -Message "Sending $ReportType report"
Send-Email -Subject $Subject -Body $Body
$DCs = "dc00", "dc01", "dc02", "dc03", "dc04"
foreach ($DC in $DCs)
{
$Args = "/replsummary $DC.ad.<domainname>.<tld>"
$ReportType = "replsummary"
Write-Verbose -Message "Executing 'repadmin.exe $Args'"
$Reps = New-Object System.Diagnostics.ProcessStartInfo
$Reps.FileName = "repadmin.exe"
$Reps.RedirectStandardError = $true
$Reps.RedirectStandardOutput = $true
$Reps.UseShellExecute = $false
$Reps.Arguments = $Args
$RepVerboseProcess = New-Object System.Diagnostics.Process
$RepVerboseProcess.StartInfo = $Reps
$RepVerboseProcess.Start() | Out-Null
$RepVerboseProcess.WaitForExit()
$Output = $RepVerboseProcess.StandardOutput.ReadToEnd()
$Error = $RepVerboseProcess.StandardError.ReadToEnd()
$Date = ((Get-Date).ToString('MM-dd-yy'))
$Subject = "AD $ReportType report for $Date"
Write-Verbose -Message "Preparing report for $ReportType"
$ReportFile = "ADReplicationReport-$ReportType-$Date.txt"
$File = (Join-Path -Path "$env:USERPROFILE\Documents\ADReplicationReports" -ChildPath "$ReportFile")
$Output | Out-File -FilePath $File -Encoding UTF8 -Append
"Errors: $Error" | Out-File -FilePath $File -Encoding UTF8 -Append
}
$MultiServerOutput = Get-Content -Path $File -Encoding UTF8 -Delimiter `n
$Body = "Showvector: `n $MultiServerOutput `n Errors: $Error"
Write-Verbose -Message "Sending $ReportType report"
Send-Email -Subject $Subject -Body $Body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment