When managing and cleaning up your IP addresses/ranges it can be very useful to get some lists of the records you have in your DNS zones. Here’s a simple script which gets the IP addresses/hostnames of the given DNS zones and exports the results to CSV.
The script is split into two parts Variables and script main. I will walk through and explain both parts below.
Variables
#VARIABLES $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath $CSV = "$dir\DNS_A_records.csv" #parameters $DNSServer = "Some_DNS_server" $Zone1 = "myzone.local" $Zone2 = "me.myzone.local"
I use the $dir object in the paths of my scripts as this object always points to the directory which the script has been run from. This is practical as it allows you to move/copy the script around without having to change any paths. $CSV is the path to the CSV file which will be created at the end of the script run.
Remember to fill out the parameters section before you run the script! $DNSServer is the name of your DNS server. $Zone1, $Zone2… etc are the names of the DNS zones which you want to get A records from. If you have more than 2 zones to get A records from then just add more objects to match the number of zones you need and add the zone names.
Script Main
#SCRIPT MAIN clear $DNS_Zones = @() $DNS_Zones += $Zone1 $DNS_Zones += $Zone2 $hosts = @() $DNS_Zones | % { $zone = $_ Write-Host "Getting DNS A records from $zone" $DNS_A_records = @(Get-WmiObject -Class MicrosoftDNS_AType -NameSpace Root\MicrosoftDNS -ComputerName $DNSServer -Filter "ContainerName = `'$zone`'") $DNS_A_records | % { $hostA = "" | select "hostname","IPAddress" $hostA.hostname = $_.OwnerName $hostA.IPAddress = $_.IPAddress $hosts += $hostA } } $hosts = $hosts | Sort-Object @{Expression={[Version]$_.IPAddress}} $hosts | Export-Csv $CSV -NoTypeInformation -Force
In the script main I first create the array $DNS_Zones which will hold the different zone names. Then the zone names ($Zone1, $Zone2… etc.) are added to the array. If you created more than two zone objects in the variables section you must add them here too.
I now create the $hosts array which will hold the records we will wish export later. The next thing that happens is that we loop through the $DNS_Zones array. For each DNS Zone we get all A records using WMI and for each of these, a custom object with the properties hostname and IPAddress is created and added to the $hosts array.
I then sort the $hosts array by IP Address using the following code:
$hosts | Sort-Object @{Expression={[Version]$_.IPAddress}}
The @{Expression} argument allows you to add the [version] type declaration on the IPAddress property on the array elements/objects which in turn enables you to easily sort the IPs.
Finally the $hosts array is exported to CSV using the great Export-CSV powershell cmdlet.
I have copied in the full script below.I hope you find it useful. Enjoy!
################################################################################################ ##Script: Get-DNS_A_Records.ps1 ## ##Description: Gets all DNS A records from a given DNS server and exports the information #+ to a CSV file. ##Created by: Noam Wajnman ##Creation Date: April 07, 2014 ################################################################################################ #VARIABLES $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath $CSV = "$dir\DNS_A_records.csv" #parameters $DNSServer = "Some_DNS_server" $Zone1 = "myzone.local" $Zone2 = "me.myzone.local" #SCRIPT MAIN clear $DNS_Zones = @() $DNS_Zones += $Zone1 $DNS_Zones += $Zone2 $hosts = @() $DNS_Zones | % { $zone = $_ Write-Host "Getting DNS A records from $zone" $DNS_A_records = @(Get-WmiObject -Class MicrosoftDNS_AType -NameSpace Root\MicrosoftDNS -ComputerName $DNSServer -Filter "ContainerName = `'$zone`'") $DNS_A_records | % { $hostA = "" | select "hostname","IPAddress" $hostA.hostname = $_.OwnerName $hostA.IPAddress = $_.IPAddress $hosts += $hostA } } $hosts = $hosts | Sort-Object @{Expression={[Version]$_.IPAddress}} $hosts | Export-Csv $CSV -NoTypeInformation -Force