Update to PowerShell Active Directory DC Health Check

This script is an update to a previous DC Health PowerShell script that requires the Quest AD module.  The below script does not require any additional modules to be installed but simply requires the ActiveDirectory module that is installed on any 2008 DC.  You can, of course, use a remote session to connect to your domain controller to perform this from your admin workstation.

Import-Module ActiveDirectory

#DC Log Archive location: \\COMPUTERNAME\c$\Windows\System32\winevt\Logs

#Routine to highlight errors in the script
filter colorize-row{
    param([string]$color="red",
    [string]$prop="Free Space (%)") #Property of the table to highlight; must be exactly the same as defined in the hashtable below

    $fgc=[console]::ForegroundColor; #Save current console color

    #test to see if the drive free space is below 20%
    if ($_.$prop -lt 20) { [console]::ForegroundColor=$color; $_ } #Set the color to $color; then output the property string
    else{ $_ } #otherwise, just output the property string

    [console]::ForegroundColor=$fgc; # revert to saved console colors
}

#Get Forest DCs
$DCs = @() #Initialize the DC array
$Forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$DomainList = $forest.domains
$DomainList | % {
  $DCs += $_.DomainControllers | select Name
}

$DCs | % { 

  $Name = $_.name
  $PingHost = Test-Connection -computername $Name -quiet

  echo "DC Name: $Name"
  if (!$Pinghost) { write-host "Return Ping: $Pinghost" -foreground Red } else { echo "Return Ping: $Pinghost" }

  try {
    $ErrorActionPreference = "Stop"; #Throw a terminating error for a non-terminating error (can't contact server)
    Get-WmiObject win32_logicaldisk -computername $Name | Where-Object { $_.DriveType -eq 3 } | select @{label="Drive";expression={$_.deviceid}}, @{label="Free Space (%)";expression={[Math]::Round(($_.FreeSpace/$_.Size)*100, 0)}} | colorize-row | fl
  }
  catch { #write the error message to the console
    'Error: {0}' -f $_.Exception.Message
  }
  finally { #reset the error action back to continue to keep running the script
    $ErrorActionPreference = "Continue"; #Reset the error action pref to default
  }
}

$workfile = repadmin.exe /showrepl * /csv 
$results = ConvertFrom-Csv -InputObject $workfile | where {$_.'Number of Failures' -ge 1}

#Here you set the tolerance level for the report
$results = $results | where {$_.'Number of Failures' -gt 1 }

if ($results -ne $null ) {
    $results = $results | select "Source DSA", "Naming Context", "Destination DSA" ,"Number of Failures", "Last Failure Time", "Last Success Time", "Last Failure Status" | ConvertTo-Html
    } else {
    $results = "There were no Replication Errors"
}

$results | out-file repl.html

.\repl.html

2 Comments

Leave a Reply