Tuesday, October 18, 2016

PowerShell v3 Geolocation with PowerShell

,
To help troubleshoot some network connectivity issues we have been seeing at work I threw together a simple client side forms script that techs can use to test basic connectivity. Sure, I know, Windows has a ton of these. In our case, WCF poses a problem, and, we have to be within a certain window or else the client connectvity fails. One component of this solution involves a quick Geolocation snippet. This can be used to ensure the ISP WAN used by the customer is within the expected time zone in order to eliminate weirdness issues that could arise from simple protocol discrepancies.

Two simple commands can be used to get the key pieces: 1) WAN IP address and 2) geolocation information. To get the WAN IP, use this approach:
$webclient = New-Object -TypeName System.Net.WebClient;
$wanip = $webclient.DownloadString(http://automation.whatismyip.com/n09230945.asp);
In case youre wondering what this URL, http://automation.whatismyip.com/n09230945.asp, is, it is a simple IP address for the full page content. When I assign its value to the $wanip variable, I can then use it in a call to get geolocation information. An easy, free geolocation web service is:
http://freegeoip.net/static/index.html
As noted on their page, you can simply get a different format (chose from json, xml,csv) and go from there. To use this in PowerShell, do this:
$geolocation = [xml] $webclient.DownloadString("http://freegeoip.net/xml/$wanip");
     
# Report IP information
$textBox1.Text += "WAN IP: $($geolocation.response.Ip)" + [Environment]::NewLine;
$textBox1.Text += "City: $($geolocation.response.City)" + [Environment]::NewLine;
$textBox1.Text += "State: $($geolocation.response.RegionCode)" + [Environment]::NewLine;
$textBox1.Text += "Zip: $($geolocation.response.ZipCode)" + [Environment]::NewLine;
$textBox1.Text += "Country: $($geolocation.response.CountryName) ($($geolocation.response.CountryCode))" + [Environment]::NewLine;
$textBox1.Text += "Latitude: $($geolocation.response.Latitude)" + [Environment]::NewLine;
$textBox1.Text += "Longitude: $($geolocation.response.Longitude)" + [Environment]::NewLine;
$textBox1.Text += "Area code: $($geolocation.response.MetroCode)" + [Environment]::NewLine;
This retrieves the content at  http://freegeoip.net/xml/192.192.192.19(a fake IP of course) into an XML data structure. I then reference the nodes on this page accordingly:

  • IP Address: $geolocation.response.Ip
  • City: $geolocation.response.City
  • State: $geolocation.response.RegionCode
  • Zip: $geolocation.response.ZipCode
  • Country Name:  $geolocation.response.CountryName
  • Country Code:  $geolocation.response.CountryCode
  • Latitude:  $geolocation.response.Latitude
  • Longitude:  $geolocation.response.Longitude
  • Metro Code:  $geolocation.response.MetroCode
When I run it, I find it pops in less than a second.

EDIT: One of the ever wise MVPs was kind enough to point out a much better way to handle my  $textBox1.Text modification. See below:
# Report IP information
$geolocation = @"
WAN IP: $($geolocation.response.Ip)
City: $($geolocation.response.City)
State: $($geolocation.response.RegionCode)
Zip: $($geolocation.response.ZipCode)
Country: $($geolocation.response.CountryName) ($($geolocation.response.CountryCode))
Latitude: $($geolocation.response.Latitude)
Longitude: $($geolocation.response.Longitude
Area code: $($geolocation.response.MetroCode)
"@
$textBox1.Text += $geolocation
Much smarter and cooler than my approach. Thank you oh wise sir in the cloud! 


0 comments to “PowerShell v3 Geolocation with PowerShell”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger