Powershell DNS 正向查找区域列表然后检查

Powershell DNS forward lookup zone list then check

好的,我们正在为所有外部域迁移到新的 NS。当前有很多域不再有效但尚未删除。我正在尝试导出 DNS 中所有正向查找区域的列表,对它们执行 ping 操作以查看它们是否存在,并对两个不同的文件进行排序,以便我可以手动重新检查错误的名称。

首先需要导出到一个文件,以便可以将其移动到不同的位置进行测试。

导出

dnscmd /enumzones /Forward | out-file C:\temp\zones.txt

测试

$names = Get-Content "C:\temp\zones.txt"
foreach ($name in $names){
  if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
    Add-Content c:\temp\resolved.csv "$name,up"
  }
  else{
    Add-Content C:\temp\unresolved.csv "$name,down"
  }
}

问题 export 命令写入一个带有额外信息的值。 IE。

Enumerated zone list:
    Zone count = 673

 Zone name                 Type       Storage         Properties

 .                         Cache      File            
 domain1.com.au            Primary    File            
 domain2.co.nz             Primary    File            
 domain3.com.au            Primary    File

只清除文件顶部等没有问题,但我如何格式化区域列表输出以便 powershell 可以读取它?

回答 下面由 @sodawillow 标记的服务器 2012 的答案,我不得不为 2008R2 做一点柚木,但没有他的答案就无法完成。 由于导出的白色 space,我遇到了 none 名称解析的问题,因此我也搞砸了它。

#store forward lookup zones names in an array
$zoneNames = Get-WmiObject -Namespace Root\MicrosoftDNS -Class "MicrosoftDNS_Zone" | ? { $_.ContainerName -Notlike '..RootHints' -And $_.ContainerName -NotLike '..Cache' -And !$_.Reverse } | Select Name

#declare results arrays and files paths
$resolvedZones = @()
$unresolvedZones = @()
$resolvedFile = "C:\Temp\resolved.csv"
$unresolvedFile = "C:\Temp\unresolved.csv"

#for each zone name
foreach ($zoneName in $zoneNames){
$string = $zoneName.Substring(0)
$string = $string.Trim()


    #if it responds to ping
    if (Test-Connection -ComputerName "$string" -Count 2 -ErrorAction SilentlyContinue) {

        #build result object and add it to resolved zones array
        $resolvedZones += [PSCustomObject]@{ ZoneName = $zoneName; Status = "UP" }
    } else {

        #build result object and add it to unresolved zones array
        $unresolvedZones += [PSCustomObject]@{ ZoneName = $zoneName; Status = "DOWN" }
    }
}

#export results arrays as CSV in results files (if not empty)
if($unresolvedZones.Length -gt 0) { $unresolvedZones | Export-Csv $unresolvedFile -NoTypeInformation }
if($resolvedZones.Length -gt 0) { $resolvedZones | Export-Csv $resolvedFile -NoTypeInformation }    

Note : I have mixed forward and reverse in my answer, but it demonstrates how you can retrieve and re-use DNS zone names.

有一个适当的 PowerShell cmdlet 可以列出 DNS 区域,Get-DNSServerZone

这是改编的脚本:

#store reverse lookup zones names in an array
$zoneNames = (Get-DnsServerZone | Where-Object { $_.IsReverseLookupZone -eq $true }).ZoneName

#declare results arrays and files paths
$resolvedZones = @()
$unresolvedZones = @()
$resolvedFile = "C:\Temp\resolved.csv"
$unresolvedFile = "C:\Temp\unresolved.csv"

#for each zone name
foreach ($zoneName in $zoneNames){

    #if it responds to ping
    if (Test-Connection -ComputerName $zoneName -Count 2 -ErrorAction SilentlyContinue) {

        #build result object and add it to resolved zones array
        $resolvedZones += [PSCustomObject]@{ ZoneName = $zoneName; Status = "UP" }
    } else {

        #build result object and add it to unresolved zones array
        $unresolvedZones += [PSCustomObject]@{ ZoneName = $zoneName; Status = "DOWN" }
    }
}

#export results arrays as CSV in results files (if not empty)
if($unresolvedZones.Length -gt 0) { $unresolvedZones | Export-Csv $unresolvedFile -NoTypeInformation }
if($resolvedZones.Length -gt 0) { $resolvedZones | Export-Csv $resolvedFile -NoTypeInformation }