通过shell枚举Windows DNS服务器区域属性

我想使用命令行工具列出(主)区域的configuration辅助服务器(如果有的话)。

Dnscmd接近:

dnscmd server /ZoneResetSecondaries:[list of secondaries] 

但是,我不想打破任何目前的设置,我只想审查它们。 PowerShell(甚至在Server 2012上)似乎没有帮助。

谢谢。

你是最正确的,你可以:

  1. 枚举区域并查找“主”区域
  2. 检索每个区域的区域信息

我以前写过关于使用PowerShell parsingdnscmd输出 ,这应该完成步骤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'} 

然后,您可以使用区域arrays自动查找所有主要区域:

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

只是为了给Mathias的答案留下一个替代scheme,下面是适用于任何版本的Powershell的单行代码:

 PS C:\> Get-WmiObject MicrosoftDNS_Zone -Namespace Root\MicrosoftDNS ` -ComputerName DC01 | Where ZoneType -EQ 1 | ` Select ContainerName, SecondaryServers 

(ZoneType值的参考: http : //msdn.microsoft.com/en-us/library/cc448936.aspx )

编辑:呃,如果使用旧版本的PS <3,更改pipe道中的第二个元素

| Where { $_.ZoneType -EQ 1 } |