Powershell Script. AD Replication Report.

Every Morning, I run a script to collect Replication test results from each of my domain controllers.

Although there could be some improvements, particularly with the text files I use. This is the script I run, with details of my environment taken out.

$Date = (get-date -Format “yyyy-MM-dd HHmm”)
$Output = “.\output.txt”
$tempFile = “.\Temp.txt”
$RunFrom = (get-location)
$Computer = gc env:computername

$smtpServer = “<yourSMTPServer>”
$emailFrom = “<YourSenderAddress>”
$emailTo = “<YourRecipients>”
$subject = “<YourSubjectLine>

$servers = “MyServer1″,”MyServer2″,”MyServer3”
$location = “MyLocation”
Set-Location $location

# Create text file
New-Item -ItemType file $Output -Force
add-content -path $Output -value “<YourTitleInformation>”
add-content -path $Output -value “$Date”
add-content -path $Output -value ” ”

$Servers | Foreach {

add-content -path $Output -value ” ”
add-content -path $Output -value “Server: $_”
add-content -path $Output -value “”

dcdiag /test:replications /s:$_ > $TempFile

(gc $TempFile) | ? {$_.trim() -ne “” } | set-content $TempFile

$tempContent = get-content $tempfile
add-content -path $Output -value $tempcontent

}

add-content -path $Output -value ” ”
add-content -path $Output -value “Run from $location on computer $computer at $date”

# Email
$OutputRead = (get-content $Output)
$body = New-object System.Text.StringBuilder
foreach($line in $OutputRead)
{
[void] $body.AppendLine($line.ToString())
}

# Send Email
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body.ToString())

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s