PowerShell – Edit, add/remove DNS Host A entry

Posted: November 19, 2019 in Scripts

This script will ping IP 1.1.1.1, if ping fails, and if ping to 2.2.2.2 is successful, and if there is host A record for IP 1.1.1.1,it will change DNS record to match 2.2.2.2

function Switch-IP {
    [CmdletBinding()]
    param (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
    [string]$IP,[string] $newIP
    )
  
    $currentIP = $oldobj.RecordData | select IPv4Address
    if ($currentIP.IPv4Address.IPAddressToString -eq $IP) {
         
    $newobj = get-dnsserverresourcerecord -name "nagios" -zonename "test.local" -rrtype "A"
    $updateip = $newIP
    $newobj.recorddata.ipv4address=[System.Net.IPAddress]::parse($updateip)
    Set-dnsserverresourcerecord -newinputobject $newobj -OldInputObject $oldobj -zonename "test.local" -passthru > C:\dnsfailover.log
    add-content -path C:\dnsfailover.log -Value $(Get-Date)
    }
    }

 $IP = "1.1.1.1"
 $newIP = "2.2.2.2"
 
    if (-Not (test-connection $IP -Quiet -Count 1)) {
    if (test-connection $newIP -Quiet -Count 1) { 

    $oldobj = get-dnsserverresourcerecord -name "nagios" -zonename "test.local" -rrtype "A"
 
    $currentIP = $oldobj.RecordData | select IPv4Address
    if ($currentIP.IPv4Address.IPAddressToString -eq $IP) {
         
    Switch-IP $IP $newIP
      
    }
 
}}
 
 
elseif ((test-connection $newIP -Quiet -Count 1) -and (test-connection $IP -Quiet -Count 1)) {
 
  
    $oldobj = get-dnsserverresourcerecord -name "nagios" -zonename "test.local" -rrtype "A"
 
    $currentIP = $oldobj.RecordData | select IPv4Address
    if ($currentIP.IPv4Address.IPAddressToString -eq $newIP) {
       
    Switch-IP $newIP $IP    
        
  }
 }
 

Next example assumes that there are 2 DNS records with same name (nagios), one record has IP 1.1.1.1, and second one is 2.2.2.2.

If ping to 1.1.1.1 fails, and if there are 2 DNS entries for host nagios, remove DNS entry for IP 1.1.1.1. When connection is restored, add back DNS entry for 1.1.1.1

$dnszone = "test.local"
$currentRecord = Get-DnsServerResourceRecord -name "nagios" -ZoneName $dnszone -RRType A 
$currentIP = $currentRecord.RecordData | select IPv4Address
$primaryIP = "1.1.1.1"
$secondaryIP = "2.2.2.2"    
    
    if (-Not (test-connection $primaryIP -Quiet -Count 1)) {
 
       if (test-connection $secondaryIP -Quiet -Count 1) { 
 
 
             if ($currentIP.Count -eq 2)  {
 
          try {
 
         Remove-DnsServerResourceRecord -name "nagios" -ZoneName $dnszone -RRType A -RecordData $primaryIP -Force -ErrorAction Stop -PassThru  > C:\dnsfailover.log  
         add-content -path C:\dnsfailover.log -Value "$(Get-Date):DNS entry removed" 
            }
 
          catch { 
          $Error[0].Exception.Message
                 }
 
 
    }}}
 
 
 
    elseif ((test-connection $secondaryIP -Quiet -Count 1) -and (test-connection $primaryIP -Quiet -Count 1)) {
 
  
           $currentRecord = Get-DnsServerResourceRecord -name "nagios" -ZoneName $dnszone -RRType A 
           $currentIP = $currentRecord.RecordData | select IPv4Address
    
        if  ($currentIP.Count -ne 2) {
 
        try {
 
         Add-DnsServerResourceRecord -A -Name "nagios" -ZoneName $dnszone -IPv4Address $primaryIP -TimeToLive 00:00:10 -PassThru  > C:\dnsfailover.log  
         add-content -path C:\dnsfailover.log -Value "$(Get-Date):DNS entry added" 
         }
 
         catch {
         $Error[0].Exception.Message
              }
       
      
  }
  
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s