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

时间:2015-11-27 04:33:37

标签: powershell dns

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

首先需要导出到文件,以便将其移动到其他位置进行测试。

导出

$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"
  }
}

测试

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

问题 export命令使用额外信息写入值。 I.E.

#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 }    

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

答案 对于@sodawillow下面标记的Server 2012的答案,我不得不为2008R2做一点柚木,但是不能用他的答案完成它。 我也搞砸了一点,因为我遇到的问题是由于导出的空白区域没有解析任何名称。

{{1}}

1 个答案:

答案 0 :(得分:0)

  

注意:我在答案中混合了前进反向,但它   演示了如何检索和重用DNS区域名称。

有一个适当的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 }