Skip to content

Script to batch resolve hostnames to IPs #21

@7MinSec

Description

@7MinSec

It's good!

$filePath = "hosts.txt"
$exportPath = "output.csv"
$hostnames = Get-Content $filePath

$table = @()

foreach ($hostname in $hostnames) {
    try {
        $ipAddress = [System.Net.Dns]::GetHostAddresses($hostname) | Select-Object -ExpandProperty IPAddressToString
        $table += [PSCustomObject]@{
            Hostname = $hostname
            IPAddress = $ipAddress
        }
    }
    catch {
        $table += [PSCustomObject]@{
            Hostname = $hostname
            IPAddress = 'N/A'
        }
    }
}

# Sort the table by IP address
$table = $table | Sort-Object IPAddress

# Export the sorted table to CSV
$table | Export-Csv -Path $exportPath -NoTypeInformation

Metadata

Metadata

Assignees

No one assigned

    Labels

    v1.2Changes for version 1.2 of the eBook

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions