Skip to content

Instantly share code, notes, and snippets.

@pstakuu
Created August 12, 2022 14:12
Show Gist options
  • Select an option

  • Save pstakuu/2383b53bdf244389d13a60368ae377e3 to your computer and use it in GitHub Desktop.

Select an option

Save pstakuu/2383b53bdf244389d13a60368ae377e3 to your computer and use it in GitHub Desktop.
resolve IP address list to hostnames
$IPs = import-csv C:\Scripts\Input\Dev_IPs_Final.csv
$zones = Get-DnsServerZone
#decided to use function instead of adding the results to an array
#when I did the array and tried to export to csv, it was all pissy because CSV needed objects and it kept outputting the line length
function Resolve-IPs{
param (
$IPs,
$zones
)
foreach ($z in $cdmig)
{
foreach ($i in $IPs)
{
$allips=$i.ip.Split(",")
if($allips.Count -gt "1")
{
#adding a temporary array here to store the string return
$temp = @()
foreach ($a in $allips)
{
$record = Get-DnsServerResourceRecord -ZoneName $z.ZoneName -RRType A | ? {$_.recorddata.ipv4address -like $a}
#make sure that a record was found, otherwise don't add it
if ($record) {
#adding the hostname to the IP address in the format HOSTNAME:IPADDRESS
$temp += "$($record.hostname):$a"
}
}
#this is where it keeps all the same IPs on the same line --- by joining the temporary array together with commas!
New-Object -TypeName PSObject -Property @{IP = $temp -join ","}
#also by using function you can just create the object and it'll be outputted by the function instead of having to store it
}
else
{
$record = Get-DnsServerResourceRecord -ZoneName $z.ZoneName -RRType A | ? {$_.recorddata.ipv4address -like $i.ip}
#if its only one entry it'll just output it
New-Object -TypeName PSObject -Property @{IP = "$($record.hostname):$($i.ip)"}
#again no storing it, just use the output later
}
}
}
}
Resolve-IPs -IPs $IPs -zones $zones | export-csv C:\scripts\output\iptohostname.csv -NoTypeInformation
#change zones param if you want to filter zones down
#$netbiosname = $zones | where {$_.zonename -eq "company.com"}
#checking the import csv to make sure it wokrs.
$output = import-csv C:\scripts\output\iptohostname.csv
#just some pull practice
#another after Branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment