Windows – Enumerate Windows DNS Server Zone attributes via shell

domain-name-systempowershellwindowswindows-server-2008

I'd like to use a command-line tool to list a (primary) zone's configured secondary servers, if any.

Dnscmd comes close with:

dnscmd server /ZoneResetSecondaries:[list of secondaries]

But, I don't want to clobber any current settings, I just want to review them. PowerShell (even on Server 2012) doesn't seem to help.

Thanks.

Best Answer

You are most correct in that you can:

  1. Enumerate the zones and look for the "Primary" ones
  2. Retrieve zone info for each zone

I've previously written about parsing dnscmd output using PowerShell, and this should accomplish step 1:

function Get-DNSZones
{
    param(
    [String]$ComputerName = "."
    )

    $enumZonesExpression = "dnscmd $ComputerName /enumzones"
    $dnscmdOut = Invoke-Expression $enumZonesExpression
    if(-not($dnscmdOut[$dnscmdOut.Count - 2] -match "Command completed successfully."))
    {
        Write-Error "Failed to enumerate zones"
        return $false
    }
    else
    {
        # The output header can be found on the fifth line: 
        $zoneHeader = $dnscmdOut[4]

        # Let's define the the index, or starting point, of each attribute: 
        $d1 = $zoneHeader.IndexOf("Zone name")
        $d2 = $zoneHeader.IndexOf("Type")
        $d3 = $zoneHeader.IndexOf("Storage")
        $d4 = $zoneHeader.IndexOf("Properties")

        # Finally, let's put all the rows in a new array:
        $zoneList = $dnscmdOut[6..($dnscmdOut.Count - 5)]

        # This will store the zone objects when we are done:
        $zones = @()

        # Let's go through all the rows and extrapolate the information we need:
        foreach($zoneString in $zoneList)
        {
            $zoneInfo = @{
                Name       =   $zoneString.SubString($d1,$d2-$d1).Trim();
                ZoneType   =   $zoneString.SubString($d2,$d3-$d2).Trim();
                Storage    =   $zoneString.SubString($d3,$d4-$d3).Trim();
                Properties = @($zoneString.SubString($d4).Trim() -split " ")
                }
            $zoneObject = New-Object PSObject -Property $zoneInfo
            $zones += $zoneObject
        }

        return $zones
    }
}

Now you can list all Primary zones with:

Get-DNSZones|Where-Object {$_.ZoneType -eq 'Primary'}

You could then use the Zone array to automate the lookup for all the primary zones:

$PrimaryZones = Get-DNSZones|Where-Object {$_.ZoneType -eq 'Primary'}
$PrimaryZones |% {$out = iex "dnscmd . /ZoneInfo $($_.ZoneName) |find `"Zone Secondaries`" "; "$($_.ZoneName) = $out"}