WMI 查询以查找来自 DNS 服务器的重复条目

WMI query to find duplicate entries from DNS servers

我想编写一个 PowerShell 脚本来查询 DNS 中的重复记录,如下所示。但是这个脚本返回了几条具有相同主机名的记录。相反,我想获取具有相同 IP 地址的不同主机名。

$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'