Created
December 8, 2021 12:37
-
-
Save brettmillerb/d5d0af30a426fae91b9a0bcc46fd7ca5 to your computer and use it in GitHub Desktop.
AD Health Check Report - Really Old Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param( | |
[String]$domainname = 'domain.com', | |
[String]$reportpath = "E:\ADHealthReport\", | |
[String]$percentlow = 10, | |
[String]$sendemailtime = "07.00A" | |
) | |
# Creating report | |
$reportName = "$($domainname.ToUpper())_ADreport $(Get-Date -Format "dd MMM yy 'at' hh.mmt").html" | |
$ADreport = $reportpath + $reportName | |
# Deleting reports older than 7 days | |
$Daysback = "-7" | |
$CurrentDate = Get-Date; | |
$DateToDelete = $CurrentDate.AddDays($Daysback); | |
Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item; | |
#Set colors for table cell backgrounds | |
$redColour = "#FF0000" | |
$orangeColour = "#FBB917" | |
$whiteColour = "#FFFFFF" | |
$greenColour = "Green" | |
# Creating Array | |
[array]$setDiskSpace = $null | |
# Create and write HTML Header of report | |
$titleDate = Get-Date -UFormat "%m-%d-%Y_%H:%M" | |
$header = " | |
<html> | |
<head> | |
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'> | |
<title>Active Directory Health Report</title> | |
<STYLE TYPE='text/css'> | |
<!-- | |
td { | |
font-family: Tahoma; | |
font-size: 11px; | |
border-top: 1px solid #999999; | |
border-right: 1px solid #999999; | |
border-bottom: 1px solid #999999; | |
border-left: 1px solid #999999; | |
padding-top: 0px; | |
padding-right: 0px; | |
padding-bottom: 0px; | |
padding-left: 0px; | |
} | |
body { | |
margin-left: 5px; | |
margin-top: 5px; | |
margin-right: 0px; | |
margin-bottom: 10px; | |
table { | |
border: thin solid #000000; | |
} | |
--> | |
</style> | |
</head> | |
<body> | |
<table width='100%'> | |
<tr bgcolor='#5698b8'> | |
<td colspan='7' height='25' align='center'> | |
<font face='tahoma' color='#003399' size='4'><strong>Active Directory Report for $titledate</strong></font> | |
</td> | |
</tr> | |
</table> | |
" | |
Add-Content $ADreport $header | |
# Create and write Table header for report | |
$domains = (Get-ADForest -Identity $domainname).Domains | |
foreach ($domain in $domains) { | |
$targetdomain = get-addomaincontroller -domain $domain -discover | |
$targetdc = $($targetdomain.name) | |
$DCs = get-addomainController -filter * -server $targetdc | |
# Create and write Table header for report | |
$tableHeader = " | |
<table width='100%'> | |
<tr bgcolor='#5698b8'> | |
<td align='center'> | |
<font face='Arial' color='#15161e' size='4'><strong>$domain</strong></font> | |
</td> | |
</tr> | |
</table> | |
" | |
Add-Content $ADreport $tableHeader | |
$tableHeader = " | |
<table width='100%'><tbody> | |
<tr bgcolor=#CCCCCC> | |
<td align='center'>DCName</td> | |
<td align='center'>Ping</td> | |
<td align='center'>Netlogon Service</td> | |
<td align='center'>NTDS Service</td> | |
<td align='center'>DNS Service</td> | |
<td align='center'>Dcdiag_netlogons</td> | |
<td align='center'>Dcdiag_Services</td> | |
<td align='center'>Dcdiag_replications</td> | |
<td align='center'>Dcdiag_FSMOCheck</td> | |
<td align='center'>DCdiag_Advertising</td> | |
<td align='center'>DiskSpace</td> | |
</tr> | |
" | |
Add-Content $ADreport $tableHeader | |
foreach ($dc in $dcs) { | |
$Dcserver = $dc.hostname | |
#Settings colours back to null | |
$coloursetping = "" | |
$coloursetnetlogon = "" | |
$coloursetntds = "" | |
$coloursetdcdns = "" | |
$coloursetdcdiagnetlogon = "" | |
$coloursetdcdiagreplications = "" | |
$coloursetdcdiagFsmoCheck = "" | |
$coloursetdcdiagAdvertising = "" | |
$colourdiskspace = "" | |
$dataRow = $NULL | |
$setDiskSpace = "" | |
#Pinging Domain Controller | |
if (Test-Connection -ComputerName $Dcserver -Count 4 -Quiet) { | |
try { | |
$setping = "ok" | |
$coloursetping = $greenColour | |
#Netlogon Service Status | |
$DcNetlogon = Get-Service -ComputerName $Dcserver -Name "Netlogon" -ErrorAction SilentlyContinue | |
if ($DcNetlogon.Status -eq "Running") { | |
$setnetlogon = "ok" | |
$coloursetnetlogon = $greenColour | |
} | |
else { | |
$setnetlogon = "$DcNetlogon.status" | |
$coloursetnetlogon = $redColour | |
} | |
#NTDS Service Status | |
if ($dc.OperatingSystem -ne "Windows Server 2003") { | |
$dcntds = Get-Service -ComputerName $Dcserver -Name "NTDS" -ErrorAction SilentlyContinue; | |
if ($dcntds.Status -eq "running") { | |
$setntds = "ok" | |
$coloursetntds = $greenColour | |
} | |
else { | |
$setntds = "$dcntds.status" | |
$coloursetntds = $redColour | |
} | |
} | |
Else { | |
$setntds = "Skipped Windows 2003" | |
$coloursetntds = $greenColour | |
} | |
#DNS Service Status | |
$dcdns = Get-Service -ComputerName $Dcserver -Name "DNS" -ea SilentlyContinue | |
if ($dcdns.Status -eq "running") { | |
$setdcdns = "ok" | |
$coloursetdcdns = $greenColour | |
} | |
else { | |
$setdcdns = "$dcdns.Status" | |
$coloursetdcdns = $redColour | |
} | |
#Dcdiag netlogons Checking now | |
$dcdiagnetlogon = dcdiag /test:netlogons /s:$dcserver | |
if ($dcdiagnetlogon -match "passed test NetLogons") { | |
$setdcdiagnetlogon = "ok" | |
$coloursetdcdiagnetlogon = $greenColour | |
} | |
else { | |
$setdcdiagnetlogon = $dcdiagnetlogon | |
$coloursetdcdiagnetlogon = $redColour | |
} | |
#Dcdiag services check | |
$dcdiagservices = dcdiag /test:services /s:$dcserver | |
if ($dcdiagservices -match "passed test services") { | |
$setdcdiagservices = "ok" | |
$coloursetdcdiagservices = $greenColour | |
} | |
Elseif ($dcdiagservices -match "WIN32_OWN_PROCESS") { | |
$setdcdiagservices = "ok Windows 2003" | |
$coloursetdcdiagservices = $greenColour | |
} | |
Else { | |
$setdcdiagservices = $dcdiagservices | |
$coloursetdcdiagservices = $redColour | |
} | |
#Dcdiag Replication Check | |
$dcdiagreplications = dcdiag /test:Replications /s:$dcserver | |
if ($dcdiagreplications -match "passed test Replications") { | |
$setdcdiagreplications = "ok" | |
$coloursetdcdiagreplications = $greenColour | |
} | |
else { | |
$setdcdiagreplications = $dcdiagreplications | |
$coloursetdcdiagreplications = $redColour | |
} | |
#Dcdiag FSMOCheck Check | |
$dcdiagFsmoCheck = dcdiag /test:FSMOCheck /s:$dcserver | |
if ($dcdiagFsmoCheck -match "passed test FsmoCheck") { | |
$setdcdiagFsmoCheck = "ok" | |
$coloursetdcdiagFsmoCheck = $greenColour | |
} | |
else | |
{$setdcdiagFsmoCheck = $dcdiagFsmoCheck} | |
#Dcdiag Advertising Check | |
$dcdiagAdvertising = dcdiag /test:Advertising /s:$dcserver | |
if ($dcdiagAdvertising -match "passed test Advertising") { | |
$setdcdiagAdvertising = "ok" | |
$coloursetdcdiagAdvertising = $greenColour | |
} | |
else { | |
$setdcdiagAdvertising = $dcdiagAdvertising | |
$coloursetdcdiagAdvertising = $redColour | |
} | |
#Disk space on C: | |
$disks = Get-WmiObject -ComputerName $dcserver -Class Win32_LogicalDisk -Filter "DriveType = 3" | |
foreach($disk in $disks){ | |
$deviceID = $disk.DeviceID; | |
$volName = $disk.VolumeName; | |
[float]$size = $disk.Size; | |
[float]$freespace = $disk.FreeSpace; | |
$percentFree = [Math]::round(($freespace / $size) * 100, 2); | |
$sizeGB = [Math]::Round($size / 1073741824, 2); | |
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2); | |
$usedSpaceGB = $sizeGB - $freeSpaceGB | |
if($percentFree -lt $percentlow) { | |
$setDiskSpace += "$deviceID$percentFree% " | |
$colourdiskspace = $redColour | |
} | |
if($colourdiskspace -ne $redColour) { | |
$colourdiskspace = $greenColour | |
$setDiskSpace = "ok" | |
} | |
# Write-host "setdiskspace set to $setdiskspace" | |
} | |
} | |
catch { $ErrorMessage = $_.Exception.Message } | |
} | |
else { | |
$setping = "Server is down" | |
$coloursetping = $redColour | |
$setnetlogon = "failed" | |
$coloursetnetlogon = $redColour | |
$setntds = "failed" | |
$coloursetntds = $redColour | |
$setdcdns = "failed" | |
$coloursetdcdns = $redColour | |
$setdcdiagnetlogon = "failed" | |
$coloursetdcdiagnetlogon = $redColour | |
$setdcdiagservices = "failed" | |
$coloursetdcdiagservices = $redColour | |
$setdcdiagreplications = "failed" | |
$coloursetdcdiagreplications = $redColour | |
$setdcdiagFsmoCheck = "failed" | |
$coloursetdcdiagFsmoCheck = $redColour | |
$setdcdiagAdvertising = "failed" | |
$coloursetdcdiagAdvertising = $redColour | |
$setDiskSpace = "failed" | |
$colourdiskspace = $redColour | |
} | |
$dcserver = $dcserver.ToLower() | |
$dataRow = " | |
<tr> | |
<td {border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#778899}'>$dcserver</td> | |
<td bgcolor=$coloursetping ' align='center'>$setping</td> | |
<td bgcolor=$coloursetnetlogon ' align='center'>$setnetlogon</td> | |
<td bgcolor=$coloursetntds ' align='center'>$setntds</td> | |
<td bgcolor=$coloursetdcdns ' align='center'>$setdcdns</td> | |
<td bgcolor=$coloursetdcdiagnetlogon ' align='center'>$setdcdiagnetlogon</td> | |
<td bgcolor=$coloursetdcdiagservices ' align='center'>$setdcdiagservices</td> | |
<td bgcolor=$coloursetdcdiagreplications ' align='center'>$setdcdiagreplications</td> | |
<td bgcolor=$coloursetdcdiagFsmoCheck ' align='center'>$setdcdiagFsmoCheck</td> | |
<td bgcolor=$coloursetdcdiagAdvertising ' align='center'>$setdcdiagAdvertising</td> | |
<td bgcolor=$colourdiskspace ' align='center'>$setDiskSpace</td> | |
</tr> | |
" | |
Add-Content $ADreport $dataRow; | |
Add-Content $ADreport "</body></html>" | |
} | |
} | |
If ($reportname -match $sendemailtime){ | |
Send-MailMessage -From "[email protected]"` | |
-Subject "$reportName"` | |
-To "#[email protected]"` | |
-SmtpServer "relay.fqdn.domain.com"` | |
-Body "Please see health status attached for $CurrentDate"` | |
-Attachments "$ADreport" | |
} | |
Exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment