April 27, 2015

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

© 2020 Ben Foster