Skip to content

Instantly share code, notes, and snippets.

@jwmoss
Created June 17, 2021 03:13
Show Gist options
  • Save jwmoss/9f7544c5aa10b24361b1f813ac2deb41 to your computer and use it in GitHub Desktop.
Save jwmoss/9f7544c5aa10b24361b1f813ac2deb41 to your computer and use it in GitHub Desktop.
adcheck.ps1
$reportpath = "$ENV:Temp\ADReport.htm"
if ((test-path $reportpath) -like $false) {
new-item $reportpath -type file
}
$smtphost = "RDUSMTP"
$from = "no_reply@weee.local"
$email1 = "jmoss@weee.com"
$timeout = "60"
###############################HTml Report Content############################
$report = $reportpath
Clear-Content $report
Add-Content $report "<html>"
Add-Content $report "<head>"
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $report '<title>AD Status Report</title>'
add-content $report '<STYLE TYPE="text/css">'
add-content $report "<!--"
add-content $report "td {"
add-content $report "font-family: Tahoma;"
add-content $report "font-size: 11px;"
add-content $report "border-top: 1px solid #999999;"
add-content $report "border-right: 1px solid #999999;"
add-content $report "border-bottom: 1px solid #999999;"
add-content $report "border-left: 1px solid #999999;"
add-content $report "padding-top: 0px;"
add-content $report "padding-right: 0px;"
add-content $report "padding-bottom: 0px;"
add-content $report "padding-left: 0px;"
add-content $report "}"
add-content $report "body {"
add-content $report "margin-left: 5px;"
add-content $report "margin-top: 5px;"
add-content $report "margin-right: 0px;"
add-content $report "margin-bottom: 10px;"
add-content $report ""
add-content $report "table {"
add-content $report "border: thin solid #000000;"
add-content $report "}"
add-content $report "-->"
add-content $report "</style>"
Add-Content $report "</head>"
Add-Content $report "<body>"
add-content $report "<table width='100%'>"
add-content $report "<tr bgcolor='Lavender'>"
add-content $report "<td colspan='7' height='25' align='center'>"
add-content $report "<font face='tahoma' color='#003399' size='4'><strong>Active Directory Health Check</strong></font>"
add-content $report "</td>"
add-content $report "</tr>"
add-content $report "</table>"
add-content $report "<table width='100%'>"
Add-Content $report "<tr bgcolor='IndianRed'>"
Add-Content $report "<td width='5%' align='center'><B>Identity</B></td>"
Add-Content $report "<td width='10%' align='center'><B>PingSTatus</B></td>"
Add-Content $report "<td width='10%' align='center'><B>NetlogonService</B></td>"
Add-Content $report "<td width='10%' align='center'><B>NTDSService</B></td>"
Add-Content $report "<td width='10%' align='center'><B>DNSServiceStatus</B></td>"
Add-Content $report "<td width='10%' align='center'><B>NetlogonsTest</B></td>"
Add-Content $report "<td width='10%' align='center'><B>ReplicationTest</B></td>"
Add-Content $report "<td width='10%' align='center'><B>ServicesTest</B></td>"
Add-Content $report "<td width='10%' align='center'><B>AdvertisingTest</B></td>"
Add-Content $report "<td width='10%' align='center'><B>FSMOCheckTest</B></td>"
Add-Content $report "</tr>"
#####################################Get ALL DC Servers#################################
$getForest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest()
$DCServers = $getForest | ForEach-Object { Get-ADDomainController -Discover -DomainName $_ } | ForEach-Object { Get-ADDomainController -server $_.Name -filter * }
################Ping Test######
foreach ($DC in $DCServers) {
$Identity = $DC.Name
Add-Content $report "<tr>"
if ( Test-Connection -ComputerName $identity -Count 1 -ErrorAction SilentlyContinue ) {
Write-Host $identity `t $identity `t Ping Success -ForegroundColor Green
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $Identity</B></td>"
Add-Content $report "<td bgcolor= 'Aquamarine' align=center> <B>Success</B></td>"
##############Netlogon Service Status################
$serviceStatus = Invoke-Command -scriptblock {get-service -Name "Netlogon" -ErrorAction SilentlyContinue} -ComputerName $identity
if ($serviceStatus.status -eq "Running") {
Write-Host $identity `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
} else {
Write-Host $identity `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Red
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
}
######################################################
##############NTDS Service Status################
$serviceStatus = Invoke-Command -scriptblock {get-service -Name "NTDS" -ErrorAction SilentlyContinue} -ComputerName $identity
if ($serviceStatus.status -eq "Running") {
Write-Host $identity `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
} else {
Write-Host $identity `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Red
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
}
######################################################
##############DNS Service Status################
$serviceStatus = Invoke-Command -scriptblock {get-service -Name "DNS" -ErrorAction SilentlyContinue} -ComputerName $identity
if ($serviceStatus.status -eq "Running") {
Write-Host $identity `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
} else {
Write-Host $identity `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Red
$svcName = $serviceStatus.name
$svcState = $serviceStatus.status
Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
}
######################################################
####################Netlogons status##################
$DCDiag = Invoke-Command -ComputerName $Identity -ArgumentList $Identity -scriptblock {param($identity)dcdiag.exe /test:netlogons /s:"$identity"}
if (($DCDiag | out-string).contains('failed')) {
Write-Host $identity `t Netlogons Test Failed -ForegroundColor Red
Add-Content $report "<td bgcolor= 'Red' align=center><B>NetlogonsFail</B></td>"
}
else {
Write-Host $identity `t Netlogons Test passed -ForegroundColor Green
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>NetlogonsPassed</B></td>"
}
########################################################
####################Replications status##################
$DCDiag = Invoke-Command -ComputerName $Identity -ArgumentList $Identity -scriptblock {param($identity)dcdiag.exe /test:replications /s:"$identity"}
if (($DCDiag | out-string).contains('failed')) {
Write-Host $identity `t Replications Test Failed -ForegroundColor Red
Add-Content $report "<td bgcolor= 'Red' align=center><B>NetlogonsFail</B></td>"
}
else {
Write-Host $identity `t Replications Test passed -ForegroundColor Green
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>NetlogonsPassed</B></td>"
}
########################################################
####################Services status##################
$DCDiag = Invoke-Command -ComputerName $Identity -ArgumentList $Identity -scriptblock {param($identity)dcdiag.exe /test:services /s:"$identity"}
if (($DCDiag | out-string).contains('failed')) {
Write-Host $identity `t Services Test Failed -ForegroundColor Red
Add-Content $report "<td bgcolor= 'Red' align=center><B>NetlogonsFail</B></td>"
}
else {
Write-Host $identity `t Services Test passed -ForegroundColor Green
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>NetlogonsPassed</B></td>"
}
########################################################
####################Advertising status##################
$DCDiag = Invoke-Command -ComputerName $Identity -ArgumentList $Identity -scriptblock {param($identity)dcdiag.exe /test:advertising /s:"$identity"}
if (($DCDiag | out-string).contains('failed')) {
Write-Host $identity `t Advertising Test Failed -ForegroundColor Red
Add-Content $report "<td bgcolor= 'Red' align=center><B>NetlogonsFail</B></td>"
}
else {
Write-Host $identity `t Advertising Test passed -ForegroundColor Green
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>NetlogonsPassed</B></td>"
}
########################################################
####################FSMOCheck status##################
$DCDiag = Invoke-Command -ComputerName $Identity -ArgumentList $Identity -scriptblock {param($identity)dcdiag.exe /test:FSMOCheck /s:"$identity"}
if (($DCDiag | out-string).contains('failed')) {
Write-Host $identity `t FSMOCheck Test Failed -ForegroundColor Red
Add-Content $report "<td bgcolor= 'Red' align=center><B>NetlogonsFail</B></td>"
}
else {
Write-Host $identity `t FSMOCheck Test passed -ForegroundColor Green
Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>NetlogonsPassed</B></td>"
}
########################################################
} else {
Write-Host $identity `t $identity `t Ping Fail -ForegroundColor Red
Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $Identity</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
Add-Content $report "<td bgcolor= 'Red' align=center> <B>Ping Fail</B></td>"
}
}
Add-Content $report "</tr>"
############################################Close HTMl Tables###########################
Add-content $report "</table>"
Add-Content $report "</body>"
Add-Content $report "</html>"
########################################################################################
#############################################Send Email#################################
$subject = "Active Directory Health Monitor"
$body = Get-Content $reportpath
$smtp = New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage
$msg.To.Add($email1)
$msg.from = $from
$msg.subject = $subject
$msg.body = $body
$msg.isBodyhtml = $true
$smtp.send($msg)
########################################################################################
########################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment