Dns
Powershell

When you wonder what the dns server is of e.g. Google:

nslookup
set type=ns
google.com

When you are interesting in working with powershell and DNS keep on reading:

Get-WmiObject -Namespace 'root\MicrosoftDNS' -Class MicrosoftDNS_AType -ComputerName srvdns01 -filter "Containername='corsair.com'" |Select OwnerName, ipaddress | format-table -auto

Luckily there are some commandlets that will do most of the work now for you:
go to dnsshell on codeplex there you will find the latest module. Unzip the zip file and place this into:
c:\windows\system32\WindowsPowershell\v1.0\modules

After the unzip, type into your powershell console:
import-module

The new commands are ready, if you want to see those type:
get-command -noun *dns*

E.g to delete a dns record you would type:
Get-DnsRecord "dnsname" -server 192.168.1.5 | Where {$_.Name -eq "dnsname"} | Remove-DnsObject

it will prompt to let you know if it is okay to delete the record 'dnsname' type Y and the record gets deleted.

When you want to know all the zones in a dns server the new commandlets are very useful e.g.:

get-dnszone -server ipaddressdnsserver | Select * | Out-GridView

Or if you want to know more about a particulair dns server:
get-dnsserver -server ipaddressdnsserver

When you want a list of all A and CNAME records from all the dns zones you would do something like this:
$zones = Get-DnsZone -ZoneType Primary -Filter "Reverse=$False" -Server dnsservername.intra

get-content \\server\user$\downloads\scripts\zones.txt | foreach  {
"`nGroup: $($_)" | out-file \\server\user$\downloads\scripts\dns-zones.txt -append
Get-dnsrecord -Name $_ -server dnsservername.intra | Where {($_.RecordType -eq "A" ) -or ($_.RecordType -eq "CNAME")} |Format-List * | out-file \\server\user$\downloads\scripts\dns-zones.txt -append
}

If you want to just see the dns servers on a remote computer type the following:
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName servername01 | Select-Object -Property [a-z]* | Select DNSServerSearchOrder

If you want to query the dns settings on a server and remotely change the dns settings of that server I found a nice script for doing so by Author: Boe Prox:
###########################################################################
#dnschange.ps1
#
#Author: Boe Prox
#Last Modified: 05Aug2009
#
#Queries servers for their DNS addresses, then makes the modification
#based on $arrdns and queries again for new addresses.
#
###########################################################################

Function QueryDNSBefore
{
$server >> "C:\scripts\dnsbefore.txt"
get-wmiobject Win32_networkadapterconfiguration -computername $server | where{$_.IPEnabled -match "True"} | select-object DNSServerSearchOrder >> "C:\scripts\dnsbefore.txt" 
"" >> "C:\scripts\dnsbefore.txt"
}
Function ChangeDNS
{
$dns = get-wmiobject Win32_networkadapterconfiguration -computername $server | where{$_.IPEnabled -match "True"}
$dns.setDNSServerSearchOrder($arrdns) | out-null
}
Function QueryDNSAfter
{
$server >> "C:\scripts\dnsafter.txt"
get-wmiobject Win32_networkadapterconfiguration -computername $server | where{$_.IPEnabled -match "True"} | select-object DNSServerSearchOrder >> "C:\scripts\dnsafter.txt" 
"" >> "C:\scripts\dnsafter.txt"
}

##Comment out first line and un-comment second line to read from hostfile
$servers = Read-Host "Enter ServerName"
#$servers = Get-Content "hosts.txt"

##Add IPs of DNS servers, seperate with a "," and enclose IP with ""
$arrdns = "192.168.1.4", "192.168.1.5", "192.168.1.6"

ForEach ($server in $servers)
{
If ((Get-WMIObject -computer $server Win32_OperatingSystem).Caption -like "*2003*")
{
Write-Host -foregroundcolor Green "Querying servers's DNS addresses on NIC before modification."
QueryDNSBefore
Write-Host -foregroundcolor Green "Making changes to server's DNS addresses on NIC."
ChangeDNS
Write-Host -foregroundcolor Green "Querying server's DNS addresses on NIC after modification."
QueryDNSAfter
}
}

Write-Host -foregroundcolor Yellow "Finished"

I also change this script to a little different way so I would have some nice logging and output to screen, check it out:

$ErrorActionPreference = 'SilentlyContinue'

##Add IPs of DNS servers, seperate with a "," and enclose IP with ""
$arrdns = "192.92.16.12", "192.92.16.13", "192.92.16.14"

##Add IPs of WINS servers, seperate with a "," and enclose IP with ""
$arrwins = "192.92.16.15","192.92.16.16"

$computers = Get-Content c:\users\username\Downloads\pingthese.txt
foreach($computer in $computers) {
$ping = Test-Connection -ComputerName $computer -Count 1 -Quiet
$computerName = [system.net.dns]::Resolve("$computer")
#$hostname = $computerName.HostName
$shortHostname = $computerName.HostName.Split(".")[0]
if ($ping -eq 'True') {
    if (Get-WmiObject -ComputerName $computer win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true}| Select DnsServerSearchOrder -expandproperty DnsServerSearchOrder){
        Write-Host "$shortHostname is pingable" -ForegroundColor Green
        $dnsSearchOrder = Get-WmiObject -ComputerName $computer win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true}| Select DnsServerSearchOrder -expandproperty DnsServerSearchOrder
             if ($dnsSearchOrder){Write-Host "$shortHostname dnsServers zijn  $dnsSearchOrder" -ForegroundColor Yellow
             #Write-Host "$hostname is pingable" -ForegroundColor Green
        $shortHostname | out-file c:\temp\overview-dnsBefore-computers.txt -append
        $dnsSearchOrder | out-file c:\temp\overview-dnsBefore-computers.txt -append
        #change dns servers
        $dns = get-wmiobject Win32_networkadapterconfiguration -computername $computer | where{$_.IPEnabled -match "True"}
        $dns.setDNSServerSearchOrder($arrdns) | out-null
        $dnsSearchOrderChanged = Get-WmiObject -ComputerName $computer win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true}| Select DnsServerSearchOrder -expandproperty DnsServerSearchOrder
        Write-host "$ShortHostName dnsServer are changed in $dnsSearchOrderChanged"
        $shortHostname | out-file C:\temp\overview-dnsAfter-computers.txt -append
        get-wmiobject Win32_networkadapterconfiguration -computername $Computer | where{$_.IPEnabled -match "True"} | select-object DNSServerSearchOrder| out-file c:\temp\overview-dnsAfter-computers.txt -append 
    }
    if (Get-WmiObject -ComputerName $computer Win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true} |Select WinsPrimaryServer -ExpandProperty WinsPrimaryServer) {
                $computer | out-file c:\temp\WINS-Before-computers.txt -append
                Get-WmiObject -ComputerName $computer Win32_NetWorkAdapterConfiguration |Where {$_.IpEnabled -eq $true} | Select WINSPrimaryServer,WINSSecondaryServer | out-file c:\temp\WINS-Before-computers.txt -append
                #change WINS Servers
                $wins = get-wmiobject Win32_networkadapterconfiguration -computername $computer | where{$_.IPEnabled -match "True"}
                $WinsServersPrimary = Get-WmiObject -ComputerName $computer Win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true} |Select WinsPrimaryServer -ExpandProperty WinsPrimaryServer
                $WinsServersSecondary = Get-WmiObject -ComputerName $computer Win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true} |Select WinsSecondaryServer -ExpandProperty WinsSecondaryServer 
                Write-Host "$shortHostName WINS servers are $WinsServersPrimary and $WinsServersSecondary" -ForegroundColor Yellow
                $wins.SetWINSServer($arrwins) | Out-Null
                $computer | out-file c:\temp\overview-WINSAfter-computers.txt -append 
                get-wmiobject Win32_networkadapterconfiguration -computername $Computer | where{$_.IPEnabled -match "True"} | select-object WINSPrimaryServer,WINSSecondaryServer | out-file c:\temp\overview-WINSAfter-computers.txt -append 
                $WinsServersPrimaryChanged = Get-WmiObject -ComputerName $computer Win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true} |Select WinsPrimaryServer -ExpandProperty WinsPrimaryServer
                $WinsServersSecondaryChanged = Get-WmiObject -ComputerName $computer Win32_NetWorkAdapterConfiguration | Where {$_.IpEnabled -eq $true} |Select WinsSecondaryServer -ExpandProperty WinsSecondaryServer 
                Write-Host "$shortHostName WINS servers are changed into $WinsServersPrimaryChanged and $WinsServersSecondaryChanged"
                }
                Else{Write-host "to be filled"}
    }
    else {Write-host "$shortHostname is pingable but networkcard is not reachable through wmi" 
        $shortHostName | out-file c:\temp\reachable-but-not-through-wmi.txt -append
        }
    }
else {Write-host "$Computer is not pingable so we cant reach it" -ForegroundColor Red
        $computer | out-file c:\temp\unreachable-computers.txt -append
        }
    }
    Write-Host -foregroundcolor Yellow "Finished"

When you want to change a CNAME to another host you could do as follows:

# we need 2 dns records for the change with set-dnsServerResourceRecord
$new = $old = Get-DnsServerResourceRecord -ComputerName dcserver  -ZoneName contoso.corp -Name Wacky -RRType Cname
# now we have 2 the same records and we need to change the new one:
$new.RecordData.HostNameAlias = "wacky.contoso.corp"
# That now had the right data for the set command
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName contoso.corp -ComputerName dcserver
# now verify that the change has been set:
Get-DnsServerResourceRecord -ComputerName dcserver -ZoneName contoso.corp -Name Wacky -RRType CName

On Me
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License