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:
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:$webclient = New-Object -TypeName System.Net.WebClient;$wanip = $webclient.DownloadString(http://automation.whatismyip.com/n09230945.asp);
http://freegeoip.net/static/index.htmlAs 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:
This retrieves the content at http://freegeoip.net/xml/192.192.192.192 (a fake IP of course) into an XML data structure. I then reference the nodes on this page accordingly:$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;
- 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:
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:
Much smarter and cooler than my approach. Thank you oh wise sir in the cloud!# 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.LongitudeArea code: $($geolocation.response.MetroCode)"@$textBox1.Text += $geolocation
0 comments:
Post a Comment