我想使用命令行工具列出(主)区域配置的辅助服务器(如果有)。
Dnscmd 接近:
dnscmd server /ZoneResetSecondaries:[list of secondaries]
但是,我不想破坏任何当前设置,我只想查看它们。PowerShell(即使在 Server 2012 上)似乎也无济于事。
谢谢。
答案1
您说得非常正确,因为您可以:
- 枚举区域并寻找“主要”区域
- 检索每个区域的区域信息
我之前解析dnscmd
输出使用 PowerShell,这应该可以完成步骤 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"}
答案2
为了给 Mathias 已经很棒的答案留下一个替代方案,这里有一个适用于任何版本 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 的旧版本,请将管道中的第二个元素更改为
| Where { $_.ZoneType -EQ 1 } |