Condividi tramite


PowerShell, go get the v4 IPs for these servers if you can resolve their names

Needed to update a spreadsheet for a bunch of servers.  I wanted PowerShell to just go look in DNS and try to resolve the name.  I didn’t need to get the configured IP from the server directly and I know they are all registered in DNS.  I also only wanted the v4 addresses, not v6.

Here is what I used to dump their name and IP in a text file.  I then updated the spreadsheet by hand just so I could put human eyes on the list for sanity’s sake.

You’ll need to put the server names in a text file named “servernames.txt” in the same folder as the script.

 

ipconfig /flushdns

$servernames = Get-Content servernames.txt

foreach ($name in $servernames) {

$ips = [System.Net.Dns]::GetHostAddresses($name) | select-object AddressFamily, IPAddressToString
$ip = $ips | ? {$_.AddressFamily -eq "InterNetwork"}
$ip = $ip.IPAddressToString

$string = "$name $ip"
out-file -filepath ips.txt -inputobject $string -append

$ips = $null
$ip = $null

}

 

Some of this borrowed from - https://blogs.msdn.com/b/powershell/archive/2006/06/26/647318.aspx