Resolving hostnames with PowerShell

We recently made some big changes to Fabrik's infrastructure requiring our customers to update their DNS records to point to a new IP Address.

In order to track the customers that had made the change I wrote the following PowerShell script to resolve hostnames from a CSV file:

$hostnames = Import-Csv "C:\temp\domains.csv"

for ($i = 0; $i -lt $hostnames.Count; $i++)
{
    $primaryHostname = $hostnames[$i].PrimaryHostname
    $ips = [System.Net.Dns]::GetHostAddresses($primaryHostname)

    $hostnames[$i].IPAddress = [string]$ips[0].IPAddressToString

    Write-Host "Resolved " $primaryHostname " to " $hostnames[$i].IPAddress
}

$hostnames | Export-Csv "C:\temp\domains-resolved.csv"

The CSV file was in the following format:

PrimaryHostname,IPAddress
example.com

Ben Foster

About Me

I'm a software engineer and aspiring entrepreneur with 12+ years experience in the tech industry and have worked with startups and SMB’s in areas such as healthcare, recruitment and e-commerce (I even worked in enterprise, once). I founded my first startup Fabrik in 2011.

I now head up the engineering team at Checkout.com. If you're interested in working in an exciting fin-tech company, drop me a message on Twitter.

Creative Commons Licence