4

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?

Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95
Cozmo
  • 41
  • 1
  • 1
  • 2
  • Why don't you just use dnscmd.exe, it's not PowerShell but it does what you need and can be used in a PowerShell script. – Peter Hahndorf Nov 03 '13 at 10:36
  • With 2012, there's a [DsnServer](http://technet.microsoft.com/en-us/library/jj649850.aspx) module. `Get-DNSResourceRecord -ZoneName ad.mdmarra.local` would dump all records in the domain. One free alternative is the [DnsShell](http://dnsshell.codeplex.com/) module on CodePlex. – jscott Nov 03 '13 at 10:41

3 Answers3

6

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}
Adam Bertram
  • 331
  • 1
  • 4
  • 11
6

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.

Ryan Ries
  • 55,481
  • 10
  • 142
  • 199
5

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
Vasili Syrakis
  • 4,558
  • 3
  • 22
  • 30