PowerShell Script – Generate DFS-R Health Reports and Email Them

Sure there are a few DFS-R scripts out there to choose from in various languages like batch, vbscript, and even PowerShell to name a few, but I decided to write one to fit my own personal needs at the time.

I created this so that it could be ran as a scheduled task each morning. Basically the script just lets you set an array of different replication groups to run the Health Report on, attaches them all to a single email, and then shoots that email off via an SSL connection through an Exchange server in this particular case.

Cheers.

<# 
 
 Run DFS health reports and email results
 
 Script: DFSRHealthReport.ps1
 Author: Nathan Thomas
 Date: 03/02/2017
 
#> 
 
#requires -version 2 
 
# Vars
$Date = Get-Date -UFormat "%m-%d-%Y"
$Domain = "yourFQDN.com"
$Namespace = "yourDFSNamespace"
$ReplGroup = @("nameOfReplicationGroup1", "nameOfReplicationGroup2", "nameOfReplicationGroup3")
$RefMember = @("referenceServerName1", "referenceServerName2", "referenceServerName3")
$ReportDir = "C:\DFSReports"
$MailServer = "yourMailServer.yourFQDN.com"
$MailTo = "support@yourFQDN.com" 
$MailFrom = "noreply@yourFQDN.com"
$MailAuthUser = "noreply"
$MailAuthPass = ConvertTo-SecureString "yourSecretPassword" -AsPlainText -Force
$MailAuth = New-Object System.Management.Automation.PSCredential($MailAuthUser, $MailAuthPass);
$MailSubject = "Daily DFS-R Health Report"
$MailBody = "Greetings Support,<br /><br />A pleasure to see you again as always, and I must say, you certainly are looking handsome today.<br /><br />By the way, here is your daily DFS-R Health Report for <strong>$Date</strong>.<br /><br />Have a positive and productive day,<br />Your friendly neighborhood shell script"
$ReportFiles = @()
$i = 0

foreach ($item in $ReplGroup) {
	# Generate report 
	DfsrAdmin.exe Health New /RgName:$Domain\$Namespace\$item /RefMemName:$Domain\$($RefMember[$i]) /RepName:$($ReportDir+"\Health-"+$Domain+"_"+$Namespace+"_"+$item+"-"+$Date) /FsCount:true

	# Push report file paths into array
	$ReportFiles += $($ReportDir+"\Health-"+$Domain+"_"+$Namespace+"_"+$item+"-"+$Date+".html")

 	$i++
}

# Email report
Send-MailMessage -From $MailFrom -To $MailTo -SmtpServer $MailServer -Subject $MailSubject -Body $MailBody -BodyAsHtml -Credential $MailAuth -UseSsl -Port 587 -Attachments $ReportFiles

# For god's sake man, clean up after yourself. Geeyodd wuttaya think disk space is free er sumthin?
Remove-Item $ReportFiles
Remove-Item $($ReportFiles -replace '.html$','.xml')
0Thumbs Up 0Thumbs Down

Leave a Reply

Be the First to Comment!

Notify of
wpDiscuz