Windows

通過 shell 列舉 Windows DNS 伺服器區域屬性

  • April 16, 2014

我想使用命令行工具列出(主)區域配置的輔助伺服器(如果有)。

Dnscmd 接近於:

dnscmd server /ZoneResetSecondaries:[list of secondaries]

但是,我不想破壞任何目前設置,我只想查看它們。PowerShell(甚至在 Server 2012 上)似乎沒有幫助。

謝謝。

你是最正確的,因為你可以:

  1. 列舉區域並尋找“主要”區域
  2. 檢索每個區域的區域資訊

我之前寫過關於使用 PowerShell 解析dnscmd輸出的文章,這應該完成第 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
   }
}

現在您可以列出所有主要區域:

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

然後,您可以使用 Zone 數組自動查找所有主要區域:

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

引用自:https://serverfault.com/questions/589211