Setup: Microsoft Active Directory DNS

Problem: You want to update some DNS records via PowerShell as part of a script, useful for failover situations.

Solution: Use the following PowerShell script

function set-dns ([string]$record, [string]$newip, [string]$dcserver)
{

$dnshost = $record.split(".")[0]
$dnszone = ($record -split $dnshost,2)[1].substring(1)


$o=Get-DnsServerResourceRecord -ZoneName $dnszone -RRType A -Name $dnshost -computername $dcserver
$n = $o.Clone()
$n.RecordData.ipv4address = [System.Net.IPAddress]::parse($newip)
$n
Set-dnsserverresourcerecord -newinputobject $n -oldinputobject $o -zonename $dnszone -passthru -computername $dcserver
}

# Usage example
set-dns "host.domain.subdomain.local" "192.168.0.100" dcontrl.domain.subdomain.local

Leave a Reply

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