In a previous tip we illustrated how you can use web services to retrieve information such as public holidays or the location of an IP address.
Often, you can get the very same information by contacting websites that provide XML results. Once you know such sites, you can use the new Invoke-WebRequest cmdlet introduced in PowerShell 3.0 to simply download the information you are after.
For example, if you'd like to nail down an IP address to a specific location, here's a quick and easy solution:
$MyPublicIP = (Invoke-WebRequest 'http://myip.dnsomatic.com' -UseBasicParsing).Content $html = Invoke-WebRequest -Uri "http://freegeoip.net/xml/$myPublicIP" -UseBasicParsing $content = [xml]$html.Content $content.response
In this sample, the first call to Invoke-WebRequest uses a website that provides your own public IP address. The website returns plain text.
Then, another website is contacted to translate the IP address into geo information. This website returns XML information which is why the script translates the received results to XML and returns the relevant data.
If all runs well, you get back information similar to this: