PowerShell script to obtain the health of your domain. Passed back as a single object.

Jan 5, 2021 | Scripting and programming, Server administration, Technology, Windows | 0 comments

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.


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

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.