PowerShell and GeoLocation

I just saw an article on Scott Hanselman’s blog about Geolocation and I thought that it might be useful to do in PowerShell. There is a website that hosts a database of IP’s and locations that Scott talks about. However, in order to use it, you need pass in your external routable IP Address, not the actual IP of your computer behind your firewall or whatever other device is doing NAT for you.

In order to find out your external IP, you can do a number of things. There is a website called WhatIsMyIPAddress.com that can find this inforation quite easily. With a simple WebRequest we can get this info into PowerShell.

   1: $whatIsMyIPUrl = "http://www.whatismyip.com/automation/n09230945.asp"
   2: $ip = (new-object System.Net.WebClient).DownloadString($whatIsMyIPUrl)

This sets $ip to a string containing the IP Address. You can type the URL into a browser and see what it returns.

Once we have that IP we can use a web service on another site to find the location for the IP address we just got.

   1: $ipInfoUrl = "http://api.hostip.info/?$ip"
   2: $ipInfo = [xml](new-object System.Net.WebClient).DownloadString($ipInfoUrl)
   3: $ipInfo.InnerText

$ipinfo is an XML document and you can probably slice it and dice it however you wish, but you can see the guts of the info using .InnerText. We can formalize this and make the output look pretty but I just wanted to post this quick and dirty version before I head out for Thanksgiving dinner.

Happy Thanksgiving!

Andy

Comments (1) -

Hey. I really related with your message today. Keep up the nice work. Simply remember to encompass your self with a constructive group of people that you could study from and vice versa and construct relationships with. By doing this, you'll create FAR MORE success for youself. Keep growing!

Comments are closed