Reputation: 5197
I need to generate a list of all DNS records for a specified subnet. Is there a way to do this using either the powershell DNS cmdlets, DNSCMD, or anything else, that isn't the very slow way that I am currently using?
I'm enumerating the host names in 17.(7,8,9).162.(16-238)
.
$HostList = @()
ForEach ($a in 7..9) {
ForEach ($b in 16..238) {
$DNSCheck = Resolve-DNSName "17.$a.162.$b"
$HostList += ($DNSCheck.NameHost)
}
}
Write-Host $HostList
What I've ended up using - Many thanks to Ansgar.
$ZoneName = "some.domain"
$DNSHost = "dnsserver"
$Hosts = Get-DnsServerResourceRecord $ZoneName -ComputerName $DNSHost -RRType 'A' |? {
$Address = $_.RecordData.IPV4Address.ToString()
$LastOctet = $Address.Split(".")[-1]
$Address -like "17.[7-9].162.*" -and $LastOctet -in 16..238
}
ForEach ($H in $Hosts) {
Write-Host $H.HostName
}
Upvotes: 2
Views: 13066
Reputation: 15
Here is the best way I found
$Zone = YourDomain.local
Get-DnsServerResourceRecord -ZoneName $Zone -RRType A | Where-Object {$_.RecordData.ipv4address -like "192.168.39.*"}
Upvotes: 0
Reputation: 21
Use this GridView and filter for your IP-Addresses
$server = "Your_DNS_Server"
$record = Get-WmiObject -Namespace "root\MicrosoftDNS" -ComputerName $server -Class MicrosoftDNS_AType
$record | Select DomainName,RecordData,OwnerName | Sort-Object -Property {[System.Version]$_.RecordData} | Out-GridView
Sorting IP-Addresses is done by [System.Version]
[system.version]("17.7.162.238")
Major Minor Build Revision
----- ----- ----- --------
17 7 162 238
Upvotes: -1
Reputation: 200273
Using PowerShell DNS cmdlets and borrowing code from this answer you could do something like this:
$zone = 'example.com'
$range = '17.7.162.16', '17.7.162.238'
function Addr2UInt($addr) {
$bytes = $addr.GetAddressBytes()
[array]::Reverse($bytes)
[BitConverter]::ToUInt32($bytes, 0)
}
$addrFrom = Addr2UInt ([Net.IPAddress]::Parse($range[0]))
$addrTo = Addr2UInt ([Net.IPAddress]::Parse($range[1]))
Get-DNSServerResourceRecord $zone -RRType 'A' | ? {
$addr = Addr2UInt $_.RecordData.IPv4Address;
$addrFrom -le $addr -and $addr -le $addrTo
}
IPAddress
objects can't be compared using operators like -le
, but since IP(v4) addresses are basically 32-bit numbers you can convert the bytes of an address to a number for comparison:
$bytes = $addr.GetAddressBytes()
(($bytes[0] * 256 + $bytes[1]) * 256 + $bytes[2]) * 256 + $bytes[3]
The ToUInt32()
method provides a more elegant way to do this conversion (makes it more obvious what's happening, too), but since the method expects the Least Significant Byte at the lowest index the array must be reversed first.
The function just wraps the conversion code for convenience, because I need it at 3 places in the script.
Upvotes: 2