Getting IP addresses for hostnames using nslookup in Powershell

≡放荡痞女 提交于 2019-12-04 14:34:52

Combining hostnames to addresses can be done within the loop. As a host can have multiple IP addresses, you need to take that into accout too. This will create another a loop. Like so,

$servers = get-content "path_to_the_file"
foreach ($server in $servers) {
  $addresses = [System.Net.Dns]::GetHostAddresses($server)
  foreach($a in $addresses) {
    "{0},{1}" -f $server, $a.IPAddressToString
  }
}
Chu

This will display "IP cannot resolve" if there is no DNS record.

$servers = get-content "path_to_the_file"e
foreach ($Server in $Servers)
{
    $Addresses = $null
    try {
        $Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
    }
    catch { 
        $Addresses = "Server IP cannot resolve."
    }
    foreach($Address in $addresses) {
        write-host $Server, $Address 
    }
}

You can also use nslookup in a Foreach-object loop with 'get-ADcomputer' cmdlet, assuming you have a consistent naming convention / computers are in AD.

The 'Select-Object -unique' does a nice job of stripping out all but one instance of the DC that was queried in the results.

get-adcomputer -Filter {name -like 'nameOfComputer*'} | ForEach-Object 
{nslookup $_.name} | Select-Object -Unique
M Rahbari

I just used the cmdlet provided by "Nicholas Leader".

It returned a response with every IP address for each machine that starts with XX-

Kudos!

get-adcomputer -Filter {name -like 'XX-*'} | ForEach-Object {nslookup $_.name} | Select-Object -Unique  
Mao Tian
#!/usr/bin/env bash
echo "############# Reverse DNS ##############"
while read id servername;do
    ip=$(nslookup $servername | grep ^Name -A1 | grep Address | awk '{printf ($2" ")}');
    echo "$id,$servername,$ip";
done<$1 >$2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!