2016-09-30 72 views
0

我想寫一個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 

回答

0

集團通過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'