I want to write a PowerShell script to query DNS for duplicate records as shown below. But this script is returning several records with the same hostnames. Instead of this I want to fetch a different hostname with the same IP address.
$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
}
}
Results should look something like:
Name IPAddress
Server1 10.194.111.22
Server1 10.140.111.22
Server2 10.333.19.121
Server2 10.333.131.24
What I want are different hostnames with the same IP address:
Name IPAddress
Server1 10.194.111.22
Server2 10.194.111.22
ServerA 10.333.19.121
ServerB 10.333.19.121
Group by IPAddress
instead of OwnerName
. You could also expand the results via the same pipeline and write it to the output file with Export-Csv
, so you don't have to construct the CSV data manually.
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'