Thursday, March 11, 2010

Query DNS with PowerShell

Following is a script that will enable you to query DNS for duplicate records. Be sure to change the location of the output file!


# Using WMI, retrieve all the duplicate DNS records
$DNS = Get-WmiObject -ComputerName 'DNS-Server' `
-Namespace 'root\MicrosoftDNS' `
-Class MicrosoftDNS_AType `
-Filter "ContainerName='Your Container'" | `
Group-Object OwnerName | Where-Object {$_.Count -gt 1}

# Create our CSV file to hold the data
$file = 'c:\temp\DNS.csv'
New-Item -ItemType file -Path $file -Force
Add-Content -Path $file -Value "Name,IPAddress"

# Iterate of the DNS items grabbing the name and IPAddress
foreach ($item in $DNS) {
foreach ($IPAddresses in $item.Group) {
$value = "{0},{1}" -f $item.name,$IPAddresses.IPAddress
Add-Content -Path $file -Value $value
}
}

Results should look something like:

NameIPAddress
Server110.194.111.22
Server210.140.111.22
ServerA10.333.19.121
ServerB10.333.131.24

Enjoy!

6 comments:

Matt Johnson (mwjcomputing) said...

Awesome post! Got me thinking of the other things I can do with DNS and WMI. Thanks Wes!

Unknown said...

Thanks Matt! Unfortunately, the script results (in our case) were not so pleasing… :)

wangwei said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anthony Ciovacco said...

So I ran this script on my dns server and it gives me duplicate names but no duplicate ip addresses.

Also is there a way to modify this so that it checks all zones?

Shohel Ranseo said...

Good readding this post