WMI查询确定DNS服务器重复A记录

问题描述:

我想写一个powerschell sciprt来查询DNS重复记录,如下所示。但是这个脚本正在返回具有相同主机名的A记录。取而代之,我想用相同的IP地址获取不同的主机名。WMI查询确定DNS服务器重复A记录

$DNS = Get-WmiObject -ComputerName 'DNS Server' -Namespace root\MicrosoftDNS -Class "MicrosoftDNS_AType" -Filter "ContainerName='Zone_Name'" | Group-Object OwnerName|Where-Object{$_.Count -gt1} 


# Create our CSV file to hold the data 
$file = 'C:\temp\DNS.csv' 
New-Item -ItemType file -Path $file -Force 
Add-Content -Path $file -Value "Name,IPAddress" 

# Iterate of the DNS items grabbing the name and IPAddress 
foreach ($item in $DNS) { 
    foreach ($IPAddresses in $item.Group) { 
     $value = "{0},{1}" -f $item.name,$IPAddresses.IPAddress 
     Add-Content -Path $file -Value $value 
    } 
} 

结果应该是这个样子:

Name IPAddress 
Server1 10.194.111.22 
Server1 10.140.111.22 
Server2 10.333.19.121 
Server2 10.333.131.24 

我想要做的是,这将是不同的主机名和IP地址相同:

Name IPAddress 
Server1 10.194.111.22 
Server2 10.194.111.22 
ServerA 10.333.19.121 
ServerB 10.333.19.121 

集团通过IPAddress而不是OwnerName 。您还可以通过相同的管道扩展结果并将其写入输出文件Export-Csv,因此您不必手动构建CSV数据。

Get-WmiObject -ComputerName 'DNS Server' -Namespace root\MicrosoftDNS -Class "MicrosoftDNS_AType" -Filter "ContainerName='Zone_Name'" | 
    Group-Object IPAddress | 
    Where-Object { $_.Count -gt 1 } | 
    ForEach-Object { $_.Group | Select-Object OwnerName, IPAddress } | 
    Export-Csv 'C:\temp\DNS.csv'