Wednesday, June 15, 2011

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:

    function new-dnsrecord {
    ## 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])\root\MicrosoftDNS: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) 

No comments: