Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving IP Address from hostname with PowerShell

Tags:

powershell

I am trying to get the ipaddress from a hostname using Powershell, but I really can't figure out how.

Any help?

like image 363
Sune Avatar asked Dec 28 '11 20:12

Sune


People also ask

How can I get IP address from hostname?

In an open command line, type ping followed by the hostname (for example, ping dotcom-monitor.com). and press Enter. The command line will show the IP address of the requested web resource in the response. An alternative way to call Command Prompt is the keyboard shortcut Win + R.

How do you validate IP address in PowerShell?

Type cast to [System.PowerShell Type accelerator [IPAddress] which is also an alias of System. Net. IPAddress class can validate IP Addresses if it is typecast-ed against it, and will throw an error when IP Address is out of range or is incorrect in format.

How do I use nslookup in PowerShell?

Just add the -Name parameter to the Resolve-DnsName cmdlet and pass it a computer name. It will search the locally assigned DNS server for a record with the matching computer name and return the record information.


1 Answers

You can get all the IP addresses with GetHostAddresses like this:

$ips = [System.Net.Dns]::GetHostAddresses("yourhosthere") 

You can iterate over them like so:

[System.Net.Dns]::GetHostAddresses("yourhosthere") | foreach {echo $_.IPAddressToString } 

A server may have more than one IP, so this will return an array of IPs.

like image 183
vcsjones Avatar answered Sep 21 '22 21:09

vcsjones