Active DirectoryScripting

Managing DNS records via PowerShell

I am always looking for ways to automate tasks, and I make it my goal to only use GUI interfaces if tasks are infrequent or there is no other choice. We are in process of reconfiguring several of our VMs, and as such are creating static DNS entries for the systems. I was very excited when I found this post with the below script:http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/13/manage-dns-in-a-windows-environment-by-using-powershell.aspx

    function new-dnsrecord {
    param(
        [string]$server,
        [string]$fzone,
        [string]$rzone,
        [string]$computer,
        [string]$address,
        [string]$alias,
        [string]$maildomain,
        [int]$priority,
        [switch]$arec,
        [switch]$ptr,
        [switch]$cname,
        [switch]$mx
    )
    ## check DNS server contactable
        if (-not (Test-Connection -ComputerName $server)){Throw "DNS server not found"}
    ## split the server fqdn and address
        $srvr = $server -split "."
        $addr = $address -split "."
        $rec = [WmiClass]"\$($srvr[0])rootMicrosoftDNS:MicrosoftDNS_ResourceRecord"
    ##
    ## create records
    ##
    ## A
        if ($arec){
            $text = "$computer IN A $address"
            $rec.CreateInstanceFromTextRepresentation($server, $fzone, $text)
        }
    ## CNAME
        if ($cname){
            $text = "$alias IN CNAME $computer"
            $rec.CreateInstanceFromTextRepresentation($server, $fzone, $text)
        }
    ## PTR
        if ($ptr){
            $text = "$($addr[3]).$rzone IN PTR $computer"
            $rec.CreateInstanceFromTextRepresentation($server, $rzone, $text)
        }
    ## MX
        if ($mx){
            $text = "$maildomain IN MX $priority $computer"
            $rec.CreateInstanceFromTextRepresentation($server, $fzone, $text)
        }
    }

Leave a Reply