Monitoring with PowerShell: Monitoring DNS record changes

Short one today, as I’m super busy with regular work. 🙂

At my company we use a dynamic DNS service for load balancing or failover. This is great because clients don’t notice downtime when a failover or load balancing action occurs. It makes everything smooth and fluent for clients and users of our systems.

The only problem with this is that sometimes a failover occurs and you don’t notice. Great for users, not so great for administrators; if a server crashes in the woods, did it really crash at all?

To make sure we always get notified about these situations we use the following script via our RMM system.

$DomainsToTest = @("remote.clientname.com", "clientswebsite.com")
New-item "C:\ProgramData\DNSTestLog" -ItemType Directory -erroraction SilentlyContinue -Force | out-null

$DNSHealth = foreach ($DomainToTest in $DomainsToTest) {

    Clear-DnsClientCache

    $PreviousDNS = get-content "C:\ProgramData\DNSTestLog\$($DomainToTest).txt" -ErrorAction SilentlyContinue
    if (!$PreviousDNS) { 
        write-host "No previous file found. Creating file. Compare will fail."
        "" | Out-File "C:\ProgramData\DNSTestLog\$($DomainToTest).txt"
    }
    $DNSResults = (Resolve-dnsname -name $DomainToTest -Type A -NoHostsFile).IP4Address
    $DNSResults | Out-File "C:\ProgramData\DNSTestLog\$($DomainToTest).txt"
    if ($PreviousDNS -ne $DNSResults) {
        "$DomainToTest does not equal the previous result."
    }

}

if (!$DNSHealth) {
    $DNSHealth = "Healthy"
}

And that’s it! as always, Happy PowerShelling

Leave a Reply

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.