I have used a script for a few years now to check the health of the active directory domain that I’m responsible for in work. It’s a very useful report written using PowerShell that highlights servers that need attention. There’s a problem though. One of the good kind of problems. The domain hasn’t had a single problem relating to services, FSMO or replication in about four years. So this report is largely ignored. Every few weeks I go and check it or if something goes wrong and I have any kind of suspicion it could be replication related, I’ll go glance at it really quickly but overall, it’s left there. This means that potentially something small could go wrong with a component of active directory and I could miss it. This doesn’t sit right with me. So as part of a few PowerShell scripts I’ve been writing lately, this regular task was up for review. My aim is very simple. Export the report as an object. Save that object as a JSON formatted file. Then every hour, go check that JSON file and verify there are no differences. If a difference is failed, then alert the world that something horrible has happened. The script checks the areas that need to be checked. This includes DNS, Netlogon, FSMO, replication, advertisements, NTDS, Syslog and ping. I got the original script from here. It has been modified over the years.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
Function GetDomainHealth { ###########################Define Variables $timeout = "60" $DomainHealthReport = @() #####################################Get ALL DC Servers $getForest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest() $DCServers = $getForest.domains | ForEach-Object { $_.DomainControllers } | ForEach-Object { $_.Name } ################Ping Test###### foreach ($DC in $DCServers) { $LogEntry = New-Object -TypeName PSObject $Identity = $DC if ( Test-Connection -ComputerName $DC -Count 1 -ErrorAction SilentlyContinue ) { $LogEntry | Add-Member -Type NoteProperty ` -Name Server -Value $Identity -Force $LogEntry | Add-Member -Type NoteProperty ` -Name Ping -Value "Success" -Force ##############Netlogon Service Status################ $serviceStatus = start-job -scriptblock { get-service -ComputerName $($args[0]) -Name "Netlogon" -ErrorAction SilentlyContinue } -ArgumentList $DC wait-job $serviceStatus -timeout $timeout if ($serviceStatus.state -like "Running") { $NetlogonStatus = $serviceStatus.state $LogEntry | Add-Member -Type NoteProperty ` -Name Netlogon -Value $NetlogonStatus -Force stop-job $serviceStatus } else { $serviceStatus1 = Receive-job $serviceStatus if ($serviceStatus1.status -eq "Running") { $svcName = $serviceStatus1.name $svcState = $serviceStatus1.status $LogEntry | Add-Member -Type NoteProperty ` -Name Netlogon -Value $svcState -Force } else { $svcName = $serviceStatus1.name $svcState = $serviceStatus1.status $LogEntry | Add-Member -Type NoteProperty ` -Name Netlogon -Value $svcState -Force } } ###################################################### ##############NTDS Service Status################ $serviceStatus = start-job -scriptblock { get-service -ComputerName $($args[0]) -Name "NTDS" -ErrorAction SilentlyContinue } -ArgumentList $DC wait-job $serviceStatus -timeout $timeout if ($serviceStatus.state -like "Running") { $NTDSStatus = $serviceStatus.state $LogEntry | Add-Member -Type NoteProperty ` -Name NTDS -Value $NTDSStatus -Force stop-job $serviceStatus } else { $serviceStatus1 = Receive-job $serviceStatus if ($serviceStatus1.status -eq "Running") { $svcName = $serviceStatus1.name $svcState = $serviceStatus1.status $LogEntry | Add-Member -Type NoteProperty ` -Name NTDS -Value $svcState -Force } else { $svcName = $serviceStatus1.name $svcState = $serviceStatus1.status $LogEntry | Add-Member -Type NoteProperty ` -Name NTDS -Value $svcState -Force } } <# ###################################################### ##############DNS Service Status################ $serviceStatus = start-job -scriptblock { get-service -ComputerName $($args[0]) -Name "DNS" -ErrorAction SilentlyContinue } -ArgumentList $DC wait-job $serviceStatus -timeout $timeout if ($serviceStatus.state -like "Running") { stop-job $serviceStatus } else { $serviceStatus1 = Receive-job $serviceStatus if ($serviceStatus1.status -eq "Running") { $svcName = $serviceStatus1.name $svcState = $serviceStatus1.status } else { $svcName = $serviceStatus1.name $svcState = $serviceStatus1.status } } ###################################################### #> ####################Netlogons status################## add-type -AssemblyName microsoft.visualbasic $cmp = "microsoft.visualbasic.strings" -as [type] $sysvol = start-job -scriptblock { dcdiag /test:netlogons /s:$($args[0]) } -ArgumentList $DC wait-job $sysvol -timeout $timeout if ($sysvol.state -like "Running") { $LogEntry | Add-Member -Type NoteProperty ` -Name Sysvol -Value "Timeout" -Force stop-job $sysvol } else { $sysvol1 = Receive-job $sysvol if ($cmp::instr($sysvol1, "passed test NetLogons")) { $LogEntry | Add-Member -Type NoteProperty ` -Name Sysvol -Value "Passed" -Force } else { $LogEntry | Add-Member -Type NoteProperty ` -Name Sysvol -Value "Failed" -Force } } ######################################################## ####################Replications status################## add-type -AssemblyName microsoft.visualbasic $cmp = "microsoft.visualbasic.strings" -as [type] $sysvol = start-job -scriptblock { dcdiag /test:Replications /s:$($args[0]) } -ArgumentList $DC wait-job $sysvol -timeout $timeout if ($sysvol.state -like "Running") { $LogEntry | Add-Member -Type NoteProperty ` -Name Replication -Value "Timeout" -Force stop-job $sysvol } else { $sysvol1 = Receive-job $sysvol if ($cmp::instr($sysvol1, "passed test Replications")) { $LogEntry | Add-Member -Type NoteProperty ` -Name Replication -Value "Passed" -Force } else { $LogEntry | Add-Member -Type NoteProperty ` -Name Replication -Value "Failed" -Force } } ######################################################## ####################Services status################## add-type -AssemblyName microsoft.visualbasic $cmp = "microsoft.visualbasic.strings" -as [type] $sysvol = start-job -scriptblock { dcdiag /test:Services /s:$($args[0]) } -ArgumentList $DC wait-job $sysvol -timeout $timeout if ($sysvol.state -like "Running") { $LogEntry | Add-Member -Type NoteProperty ` -Name Services -Value "Failed" -Force stop-job $sysvol } else { $sysvol1 = Receive-job $sysvol if ($cmp::instr($sysvol1, "passed test Services")) { $LogEntry | Add-Member -Type NoteProperty ` -Name Services -Value "Passed" -Force } else { $LogEntry | Add-Member -Type NoteProperty ` -Name Services -Value "Failed" -Force } } ######################################################## ####################Advertising status################## add-type -AssemblyName microsoft.visualbasic $cmp = "microsoft.visualbasic.strings" -as [type] $sysvol = start-job -scriptblock { dcdiag /test:Advertising /s:$($args[0]) } -ArgumentList $DC wait-job $sysvol -timeout $timeout if ($sysvol.state -like "Running") { $LogEntry | Add-Member -Type NoteProperty ` -Name Advertising -Value "Timeout" -Force stop-job $sysvol } else { $sysvol1 = Receive-job $sysvol if ($cmp::instr($sysvol1, "passed test Advertising")) { $LogEntry | Add-Member -Type NoteProperty ` -Name Advertising -Value "Passed" -Force } else { $LogEntry | Add-Member -Type NoteProperty ` -Name Advertising -Value "Failed" -Force } } ######################################################## ####################FSMOCheck status################## add-type -AssemblyName microsoft.visualbasic $cmp = "microsoft.visualbasic.strings" -as [type] $sysvol = start-job -scriptblock { dcdiag /test:FSMOCheck /s:$($args[0]) } -ArgumentList $DC wait-job $sysvol -timeout $timeout if ($sysvol.state -like "Running") { $LogEntry | Add-Member -Type NoteProperty ` -Name FSMO -Value "Timeout" -Force stop-job $sysvol } else { $sysvol1 = Receive-job $sysvol if ($cmp::instr($sysvol1, "passed test FsmoCheck")) { $LogEntry | Add-Member -Type NoteProperty ` -Name FSMO -Value "Passed" -Force } else { $LogEntry | Add-Member -Type NoteProperty ` -Name FSMO -Value "Failed" -Force } } } else { $LogEntry | Add-Member -Type NoteProperty ` -Name Server -Value $Identity -Force $LogEntry | Add-Member -Type NoteProperty ` -Name Ping -Value "Failed" -Force $LogEntry | Add-Member -Type NoteProperty ` -Name Netlogon -Value "Failed" -Force $LogEntry | Add-Member -Type NoteProperty ` -Name NTDS -Value "Failed" -Force $LogEntry | Add-Member -Type NoteProperty ` -Name Sysvol -Value "Failed" -Force $LogEntry | Add-Member -Type NoteProperty ` -Name Replication -Value "Failed" -Force $LogEntry | Add-Member -Type NoteProperty ` -Name Services -Value "Failed" -Force $LogEntry | Add-Member -Type NoteProperty ` -Name Advertising -Value "Failed" -Force $LogEntry | Add-Member -Type NoteProperty ` -Name FSMO -Value "Failed" -Force } $DomainHealthReport += $LogEntry } Return $DomainHealthReport } GetDomainHealth |
0 Comments