Getting Geo Location of Any IP Address or Hostname in PowerShell

2

FreeGeoIP.net is a free web app that provides a public HTTP API for software developers to search the geo-location of any given IP address or hostname. It uses a database of IP addresses associated with cities along with some other relevant information such as time zone, latitude, and longitude, etc. You can supply a hostname or IP address to get the result in 3 formats, CSV, XML, and JSON.

The syntax is simply like this, and you can use it right in any of the browsers you use.

http://freegeoip.net/{format}/{IP_or_hostname}

in which, format can be one of "CSV", "XML", or "JSON"

For example, to get Geolocation of our web server in XML in a browser, you do:

freegeoip.net xml www.nextofwindows.com 2015 04 23 11 33 30 - Getting Geo Location of Any IP Address or Hostname in PowerShell

Now let’s see how we can do it in PowerShell way.

Invoke-RestMethod is a PowerShell cmdlet that deals with HTTP/HTTPS requests related to Representational State Transfer (REST) web services that return richly structured data, such as XML or JSON. It’s a perfect candidate to perform the call and display the result in a nicely formatted way.

So, to get the geolocation info of our web server in PowerShell, you can just run the following cmdlet in the PowerShell window.

Invoke-RestMethod -Method Get -Uri http://freegeoip.net/xml/www.nextofwindows.com/ | Select-Object -Property InnerXML | Out-Gridview

Invoke RestMethod Method Get URI http   freegeoip.net xml www.nextofwindows.co 2015 04 23 14 06 26 - Getting Geo Location of Any IP Address or Hostname in PowerShell

To put together a PowerShell script that checks any given IP address and hostname, try the following codes.

#requires -Version 3 
$source = [string]$args[0] 
$infoService = "http://freegeoip.net/xml/$source" 
$geoip = Invoke-RestMethod -Method Get -URI $infoService 
$geoip.Response

Windows PowerShell ISE 2015 04 23 14 11 04 - Getting Geo Location of Any IP Address or Hostname in PowerShell

You can also download the same script below to save some of your times. Download it to your local computer and run it in PowerShell window with the following usage:

.\ipgeo.ps1 {ip_or_hostname}

An alternative

Nekudo is another free IP geolocation API that we can use to archive this. The HTTP Get request schema of using the service is something like this:

http://geoip.nekudo.com/api/{ip}

Then you can use the Invoke-RestMethod cmdlet piped with Select-Object to retrieve the information.

Invoke-RestMethod -Method Get -Uri "http://geoip.nekudo.com/api/{ip}" | Select-Object -ExpandProperty Country -Property City, IP, Location

Here is where Google’s public DNS service lives:

PowerShell IP Geolocation with Nekudo 600x166 - Getting Geo Location of Any IP Address or Hostname in PowerShell

If you would like to find out the geolocation of a hostname, you will need to resolve the DNS info and save it in a variable first.

$IPAddress = [Net.Dns]::GetHostByName('domain-name').AddressList.IPAddressToString

Here is where Microsoft’s website lives:

PowerShell IP Geolocation with Nekudo from a hostname 600x190 - Getting Geo Location of Any IP Address or Hostname in PowerShell

Furthermore, since the dataset you retrieved from Nekudo’s API includes the latitude and longitude coordinates, you can find out where it actually using Google Maps.

To wrap it all up, the following code finds out the geolocation information about your public IP and opens it up in Google Maps.

$ip = Invoke-RestMethod -Uri http://checkip.amazonaws.com/
$geo = Invoke-RestMethod -Uri "http://geoip.nekudo.com/api/$IP"
$latitude = $geo.Location.Latitude
$longitude = $geo.Location.Longitude 
$url = "https://www.google.com/maps/preview/@$latitude,$longitude,12z"
Start-Process -FilePath $url

Another alternative

IPInfo.io is another web service that you can use the query the information about the IP. The following command returns the detail geo-location information about your current IP address.

Invoke-RestMethod -Uri 'http://ipinfo.io'

To query the info about a specific IP address, add /{ipaddress} at the end. For example, the following command reveals the geo-location information about Google Public DNS.

Invoke-RestMethod -Uri 'http://ipinfo.io/8.8.8.8'

PowerShell Geolocation with ipinfo - Getting Geo Location of Any IP Address or Hostname in PowerShell

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here