While I'm rather new to powerShell v3, there's some things I can do with it, however, one thing that has eluded me is how to pull all hosts listed in our DNS Manager (Server 2008 R2). I don't need to set or remove anything just have it query the listing into a text file. Surprisingly I'm not finding a way to do this. Anybody know how to do this, please?
3 Answers
I've used DNSShell before. http://dnsshell.codeplex.com/. To get all A records in a zone you can do this:
Get-DnsRecord -RecordType A -ZoneName FQDN -Server ServerName
To get this into a text file:
Get-DnsRecord -RecordType A -ZoneName FQDN -Server ServerName | % {Add-Content -Value $_ -Path filename.txt}
Another method I don't see mentioned yet:
Get-WmiObject -Namespace Root\MicrosoftDNS -Query "SELECT * FROM MicrosoftDNS_AType WHERE ContainerName='domain.com'"
WMI is good to remember when you can't download DnsShell for some reason, or if you're on an older version of Powershell that doesn't have the baked-in Cmdlets, or if you're targeting an older version of Windows Server.
The DnsServer module available in Windows Server 2012, Powershell v3 has the following commands that might be useful to you:
Get-DnsServerZone
Get-DnsServerResourceRecord
The first will get you all the zones The second will get you the records for whatever zone you pass to it
They are basically the equivalent of DNSCMD's /EnumZones
and /EnumRecords
.
So... You could write something like this to get ALL of the records from ALL zones:
$Zones = @(Get-DnsServerZone)
ForEach ($Zone in $Zones) {
Write-Host "`n$Zone.ZoneName" -ForegroundColor "Yellow"
$Zone | Get-DnsServerResourceRecord
}
Also, I'm fairly sure that server 2012 keeps an actual zonefile for each zone now? So you should have a file copy for all your zones.
If you're working with 2008 R2, then you can use this script which I use to back up all of my zones to files:
$zones = @( `
dnscmd /enumzones | `
select-string -pattern "\b(?i)((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b" | %{$_.Matches} | %{$_.Value};
);
ForEach ($domain in $zones) {
$backup = "dnscmd . /zoneExport $domain $domain";
Invoke-Expression $backup | Out-Null
Write-Host "Backing up $domain" -ForegroundColor "White"
};
ForEach ($item in (gci C:\Windows\System32\dns)) {
Write-Host "Renaming $item" -ForegroundColor "White"
Rename-item $item.fullname ([string]$item + ".dns")
}
Write-Host "Back up complete." -ForegroundColor "Cyan"
cmd /c pause | out-null
Get-DNSResourceRecord -ZoneName ad.mdmarra.local
would dump all records in the domain. One free alternative is the DnsShell module on CodePlex.