If you have a list of hostnames/servers that you need IP addresses for its cumbersome to ping each server and get the ip address.
PowerShell to the rescue!
To do this we need a file called Server.txt with each server’s hostname on each line. I am storing the file in D:\Data\Servers.txt.
Once we run the script below it resolves the ip via DNS and stores to another file called D:\Data\Addresses.txt.
All the IP addresses are getting pulled from their DNS value.Â
1 2 3 4 5 6 |
function Get-HostToIP($hostname) { $result = [system.Net.Dns]::GetHostByName($hostname) $result.AddressList | ForEach-Object {$_.IPAddressToString } } Get-Content "D:\Data\Servers.txt" | ForEach-Object {(Get-HostToIP($_)) >> d:\data\Addresses.txt} |