Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, December 23, 2015

Resolving IP Address from hostname with PowerShell

Resolving IP Address from hostname with PowerShell


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

Any help?

Answer by vcsjones for Resolving IP Address from hostname with PowerShell


If you want to get the IP address so you can work with the address, you can do 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.

Answer by vulcan raven for Resolving IP Address from hostname with PowerShell


Get IPv4 and IPv6 addresses from hostname in powershell is explained here. Also, you can get the script by clicking Resolve IP address from Hostname in the aforementioned article.

Answer by Robbie P for Resolving IP Address from hostname with PowerShell


I would use the solution vcsjones commented, but since the result of his solution when used on a hostname which has multiple IP addresses would be an array of IPs, this would cause problems with further ping/tracert.

The solution I use is the following:

$ping = New-Object System.Net.NetworkInformation.Ping  $ips = $($ping.Send("yourhosthere").Address).IPAddressToString  

This would display a single IP string (in the $ips variable), resolved from a single ICMP reply, preventing an array of IPs for hostnames with multiple IPs.

Answer by DNT for Resolving IP Address from hostname with PowerShell


If you know part of the subnet (i.e. 10.3 in this example), then this will grab any addresses that are in the given subnet:

PS C:\> [System.Net.Dns]::GetHostAddresses("MyPC") | foreach { $_.IPAddressToString | findstr "10.3."}  

Answer by user3770660 for Resolving IP Address from hostname with PowerShell


The simplest way:

ping hostname  

e.g.

ping dynlab938.meng.auth.gr  

it will print: Pinging dynlab938.meng.auth.gr [155.207.29.38] with 32 bytes of data

Answer by Gary Barnes for Resolving IP Address from hostname with PowerShell


This worked well for my purpose

$ping = ping -4 $env:COMPUTERNAME  $ip = $ping.Item(2)  $ip = $ip.Substring(11,11)  

Answer by Bridger for Resolving IP Address from hostname with PowerShell


this is nice and simple and gets all the nodes.

$ip = Resolve-DNSName google.com  $ip  

also try inputting an ip instead of a domain name and check out those results too!

Answer by Joe Johnston for Resolving IP Address from hostname with PowerShell


Working one liner if you want a single result from the collection:

$ipAddy = [System.Net.Dns]::GetHostAddresses("yahoo.com")[0].IPAddressToString;   

hth

Answer by dsaydon for Resolving IP Address from hostname with PowerShell


[System.Net.Dns]::GetHostAddresses("localhost")  | where {$_.AddressFamily -notlike "InterNetworkV6"} | foreach {echo $_.IPAddressToString }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.