Get your Public IP and Geographic Data with PowerShell

 

Always fascinated by what can be done very tersely with PowerShell.

I saw an interesting tip on PowerShell.com that gives the geopraphic info on a given public IP. They had a post I dug up a couple years back that determines your public IP dynamically, so I connected the Lego blocks… here is a script that determines your public IP and tells you where you are, using a web page and a REST API.

[codesyntax lang=”powershell”]

#Determine Public IP Dynamically
$ipaddress = (Invoke-WebRequest 'http://myip.dnsomatic.com' -UseBasicParsing).Content
$infoService = "http://freegeoip.net/xml/$ipaddress"
#Get Geographic Data based on my IP
$geoip = Invoke-RestMethod -Method Get -URI $infoService
$geoip.Response

#As a One-Liner
(Invoke-RestMethod -Method Get -URI "http://freegeoip.net/xml/$(Invoke-WebRequest 'http://myip.dnsomatic.com' -UseBasicParsing)").Response

[/codesyntax]

 

Social

Content

Speaking

All Rights Reserved Theme by 404 THEME.