randomThought
randomThought

Reputation: 6393

Get the IP of a server through powershell when it doesnt respond to a ping

Due to firewall constraints, the servers I deal with do not reply to a ping, but do give their IP address

for eg, if I do :

ping myhost.com

I get

Pinging myhost.com [192.1.1.1] with 32 bytes of data:
Request Timed Out.

How can I just get the IP of the server from powershell? I know it can be done various ways if a server responds to a ping (Test-Connection or Ping-Host) , but I cant figure it when it ignores ping requests

Upvotes: 2

Views: 2582

Answers (2)

dugas
dugas

Reputation: 12453

You can use the System.Net.Dns.GetHostAddresses method. This method will return an array of System.Net.IPAddress objects.

Examples of usage:

$ipAddresses = [System.Net.Dns]::GetHostAddresses('google.com')

List all addresses:

$ipAddresses | %{$_.IPAddressToString}

Grab the first address in the array:

$ipAddresses[0].IPAddressToString

Upvotes: 4

Shay Levy
Shay Levy

Reputation: 126762

You can try and resolve it by DNS:

[System.Net.Dns]::GetHostByName('google.com').AddressList

Upvotes: 1

Related Questions