# .SYNOPSIS Get-ADHealth.ps1 - Domain Controller Health Check Script. .DESCRIPTION This script performs a list of common health checks to a specific domain, or the entire forest. The results are then compiled into a colour coded HTML report. .OUTPUTS The results are currently only output to HTML for email or as an HTML report file, or sent as an SMTP message with an HTML body. .PARAMETER DomainName Perform a health check on a specific Active Directory domain. .PARAMETER ReportFile Output the report details to a file in the current directory. .PARAMETER SendEmail Send the report via email. You have to configure the correct SMTP settings. .EXAMPLE .\Get-ADHealth.ps1 -ReportFile Checks all domains and all domain controllers in your current forest and creates a report. .EXAMPLE .\Get-ADHealth.ps1 -DomainName alitajran.com -ReportFile Checks all the domain controllers in the specified domain "alitajran.com" and creates a report. .EXAMPLE .\Get-ADHealth.ps1 -DomainName alitajran.com -SendEmail Checks all the domain controllers in the specified domain "alitajran.com" and sends the resulting report as an email message. .LINK alitajran.com/active-directory-health-check-powershell-script .NOTES Written by: ALI TAJRAN Website: alitajran.com LinkedIn: linkedin.com/in/alitajran .CHANGELOG V1.00, 01/21/2023 - Initial version V1.10, 06/18/2023 - Added SMTP port to $smpsettings hashtable and date/time to $reportfilename #> [CmdletBinding()] Param( [Parameter( Mandatory = $false)] [string]$DomainName, [Parameter( Mandatory = $false)] [switch]$ReportFile, [Parameter( Mandatory = $false)] [switch]$SendEmail ) #................................... # Global Variables #................................... $now = Get-Date $date = $now.ToShortDateString() [array]$allDomainControllers = @() $reportime = Get-Date $reportemailsubject = "Domain Controller Health Report" $smtpsettings = @{ To = 'email@domain.com' From = 'adhealth@yourdomain.com' Subject = "$reportemailsubject - $now" SmtpServer = "mail.domain.com" Port = "25" } #................................... # Functions #................................... # This function gets all the domains in the forest. Function Get-AllDomains() { Write-Verbose "..running function Get-AllDomains" $allDomains = (Get-ADForest).Domains return $allDomains } # This function gets all the domain controllers in a specified domain. Function Get-AllDomainControllers ($DomainNameInput) { Write-Verbose "..running function Get-AllDomainControllers" [array]$allDomainControllers = Get-ADDomainController -Filter * -Server $DomainNameInput return $allDomainControllers } # This function tests the name against DNS. Function Get-DomainControllerNSLookup($DomainNameInput) { Write-Verbose "..running function Get-DomainControllerNSLookup" try { $domainControllerNSLookupResult = Resolve-DnsName $DomainNameInput -Type A | select -ExpandProperty IPAddress $domainControllerNSLookupResult = 'Success' } catch { $domainControllerNSLookupResult = 'Fail' } return $domainControllerNSLookupResult } # This function tests the connectivity to the domain controller. Function Get-DomainControllerPingStatus($DomainNameInput) { Write-Verbose "..running function Get-DomainControllerPingStatus" If ((Test-Connection $DomainNameInput -Count 1 -quiet) -eq $True) { $domainControllerPingStatus = "Success" } Else { $domainControllerPingStatus = 'Fail' } return $domainControllerPingStatus } # This function tests the domain controller uptime. Function Get-DomainControllerUpTime($DomainNameInput) { Write-Verbose "..running function Get-DomainControllerUpTime" If ((Test-Connection $DomainNameInput -Count 1 -quiet) -eq $True) { try { $W32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $DomainNameInput -ErrorAction SilentlyContinue $timespan = $W32OS.ConvertToDateTime($W32OS.LocalDateTime) - $W32OS.ConvertToDateTime($W32OS.LastBootUpTime) [int]$uptime = "{0:00}" -f $timespan.TotalHours } catch [exception] { $uptime = 'WMI Failure' } } Else { $uptime = '0' } return $uptime } # This function checks the DIT file drive space. Function Get-DITFileDriveSpace($DomainNameInput) { Write-Verbose "..running function Get-DITFileDriveSpace" If ((Test-Connection $DomainNameInput -Count 1 -quiet) -eq $True) { try { $key = "SYSTEM\CurrentControlSet\Services\NTDS\Parameters" $valuename = "DSA Database file" $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $DomainNameInput) $regkey = $reg.opensubkey($key) $NTDSPath = $regkey.getvalue($valuename) $NTDSPathDrive = $NTDSPath.ToString().Substring(0, 2) $NTDSPathFilter = '"' + 'DeviceID=' + "'" + $NTDSPathDrive + "'" + '"' $NTDSDiskDrive = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $DomainNameInput -ErrorAction SilentlyContinue | ? { $_.DeviceID -eq $NTDSPathDrive } $NTDSPercentFree = [math]::Round($NTDSDiskDrive.FreeSpace / $NTDSDiskDrive.Size * 100) } catch [exception] { $NTDSPercentFree = 'WMI Failure' } } Else { $NTDSPercentFree = '0' } return $NTDSPercentFree } # This function checks the DNS, NTDS and Netlogon services. Function Get-DomainControllerServices($DomainNameInput) { Write-Verbose "..running function DomainControllerServices" $thisDomainControllerServicesTestResult = New-Object PSObject $thisDomainControllerServicesTestResult | Add-Member NoteProperty -name DNSService -Value $null $thisDomainControllerServicesTestResult | Add-Member NoteProperty -name NTDSService -Value $null $thisDomainControllerServicesTestResult | Add-Member NoteProperty -name NETLOGONService -Value $null If ((Test-Connection $DomainNameInput -Count 1 -quiet) -eq $True) { If ((Get-Service -ComputerName $DomainNameInput -Name DNS -ErrorAction SilentlyContinue).Status -eq 'Running') { $thisDomainControllerServicesTestResult.DNSService = 'Success' } Else { $thisDomainControllerServicesTestResult.DNSService = 'Fail' } If ((Get-Service -ComputerName $DomainNameInput -Name NTDS -ErrorAction SilentlyContinue).Status -eq 'Running') { $thisDomainControllerServicesTestResult.NTDSService = 'Success' } Else { $thisDomainControllerServicesTestResult.NTDSService = 'Fail' } If ((Get-Service -ComputerName $DomainNameInput -Name netlogon -ErrorAction SilentlyContinue).Status -eq 'Running') { $thisDomainControllerServicesTestResult.NETLOGONService = 'Success' } Else { $thisDomainControllerServicesTestResult.NETLOGONService = 'Fail' } } Else { $thisDomainControllerServicesTestResult.DNSService = 'Fail' $thisDomainControllerServicesTestResult.NTDSService = 'Fail' $thisDomainControllerServicesTestResult.NETLOGONService = 'Fail' } return $thisDomainControllerServicesTestResult } # This function runs the five DCDiag tests and saves them in a variable for later processing. Function Get-DomainControllerDCDiagTestResults($DomainNameInput) { Write-Verbose "..running function Get-DomainControllerDCDiagTestResults" $DCDiagTestResults = New-Object Object If ((Test-Connection $DomainNameInput -Count 1 -quiet) -eq $True) { $DCDiagTest = (Dcdiag.exe /s:$DomainNameInput /test:services /test:FSMOCheck /test:KnowsOfRoleHolders /test:Advertising /test:Replications) -split ('[\r\n]') $DCDiagTestResults | Add-Member -Type NoteProperty -Name "ServerName" -Value $DomainNameInput $DCDiagTest | % { Switch -RegEx ($_) { "Starting" { $TestName = ($_ -Replace ".*Starting test: ").Trim() } "passed test|failed test" { If ($_ -Match "passed test") { $TestStatus = "Passed" # $TestName # $_ } Else { $TestStatus = "Failed" # $TestName # $_ } } } If ($TestName -ne $Null -And $TestStatus -ne $Null) { $DCDiagTestResults | Add-Member -Name $("$TestName".Trim()) -Value $TestStatus -Type NoteProperty -force $TestName = $Null; $TestStatus = $Null } } return $DCDiagTestResults } Else { $DCDiagTestResults | Add-Member -Type NoteProperty -Name "ServerName" -Value $DomainNameInput $DCDiagTestResults | Add-Member -Name Replications -Value 'Failed' -Type NoteProperty -force $DCDiagTestResults | Add-Member -Name Advertising -Value 'Failed' -Type NoteProperty -force $DCDiagTestResults | Add-Member -Name KnowsOfRoleHolders -Value 'Failed' -Type NoteProperty -force $DCDiagTestResults | Add-Member -Name FSMOCheck -Value 'Failed' -Type NoteProperty -force $DCDiagTestResults | Add-Member -Name Services -Value 'Failed' -Type NoteProperty -force } return $DCDiagTestResults } # This function checks the server OS version. Function Get-DomainControllerOSVersion ($DomainNameInput) { Write-Verbose "..running function Get-DomainControllerOSVersion" $W32OSVersion = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $DomainNameInput -ErrorAction SilentlyContinue).Caption return $W32OSVersion } # This function checks the free space on the OS drive Function Get-DomainControllerOSDriveFreeSpace ($DomainNameInput) { Write-Verbose "..running function Get-DomainControllerOSDriveFreeSpace" If ((Test-Connection $DomainNameInput -Count 1 -quiet) -eq $True) { try { $thisOSDriveLetter = (Get-WmiObject Win32_OperatingSystem -ComputerName $DomainNameInput -ErrorAction SilentlyContinue).SystemDrive $thisOSPathFilter = '"' + 'DeviceID=' + "'" + $thisOSDriveLetter + "'" + '"' $thisOSDiskDrive = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $DomainNameInput -ErrorAction SilentlyContinue | ? { $_.DeviceID -eq $thisOSDriveLetter } $thisOSPercentFree = [math]::Round($thisOSDiskDrive.FreeSpace / $thisOSDiskDrive.Size * 100) } catch [exception] { $thisOSPercentFree = 'WMI Failure' } } return $thisOSPercentFree } # This function generates HTML code from the results of the above functions. Function New-ServerHealthHTMLTableCell() { param( $lineitem ) $htmltablecell = $null switch ($($reportline."$lineitem")) { $success { $htmltablecell = "
| Server | Site | OS Version | Operation Master Roles | DNS | Ping | Uptime (hrs) | DIT Free Space (%) | OS Free Space (%) | DNS Service | NTDS Service | NetLogon Service | DCDIAG: Advertising | DCDIAG: Replications | DCDIAG: FSMO KnowsOfRoleHolders | DCDIAG: FSMO Check | DCDIAG: Services | Processing Time |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| $($reportline.server) | " $htmltablerow += "$($reportline.site) | " $htmltablerow += "$($reportline."OS Version") | " $htmltablerow += "$($fsmoRoleHTML) | " $htmltablerow += (New-ServerHealthHTMLTableCell "DNS" ) $htmltablerow += (New-ServerHealthHTMLTableCell "Ping") if ($($reportline."uptime (hrs)") -eq "WMI Failure") { $htmltablerow += "Could not test server uptime. | " } elseif ($($reportline."Uptime (hrs)") -eq $string17) { $htmltablerow += "$string17 | " } else { $hours = [int]$($reportline."Uptime (hrs)") if ($hours -le 24) { $htmltablerow += "$hours | " } else { $htmltablerow += "$hours | " } } $space = $reportline."DIT Free Space (%)" if ($space -eq "WMI Failure") { $htmltablerow += "Could not test server free space. | " } elseif ($space -le 30) { $htmltablerow += "$space | " } else { $htmltablerow += "$space | " } $osSpace = $reportline."OS Free Space (%)" if ($osSpace -eq "WMI Failure") { $htmltablerow += "Could not test server free space. | " } elseif ($osSpace -le 30) { $htmltablerow += "$osSpace | " } else { $htmltablerow += "$osSpace | " } $htmltablerow += (New-ServerHealthHTMLTableCell "DNS Service") $htmltablerow += (New-ServerHealthHTMLTableCell "NTDS Service") $htmltablerow += (New-ServerHealthHTMLTableCell "NetLogon Service") $htmltablerow += (New-ServerHealthHTMLTableCell "DCDIAG: Advertising") $htmltablerow += (New-ServerHealthHTMLTableCell "DCDIAG: Replications") $htmltablerow += (New-ServerHealthHTMLTableCell "DCDIAG: FSMO KnowsOfRoleHolders") $htmltablerow += (New-ServerHealthHTMLTableCell "DCDIAG: FSMO Check") $htmltablerow += (New-ServerHealthHTMLTableCell "DCDIAG: Services") $averageProcessingTime = ($allTestedDomainControllers | measure -Property "Processing Time" -Average).Average if ($($reportline."Processing Time") -gt $averageProcessingTime) { $htmltablerow += "$($reportline."Processing Time") | " } elseif ($($reportline."Processing Time") -le $averageProcessingTime) { $htmltablerow += "$($reportline."Processing Time") | " } [array]$serverhealthhtmltable = $serverhealthhtmltable + $htmltablerow } $serverhealthhtmltable = $serverhealthhtmltable + "