Skip to content

Instantly share code, notes, and snippets.

@kvprasoon
Last active August 30, 2015 04:36
Show Gist options
  • Save kvprasoon/c1bdd4192c3031fd29f5 to your computer and use it in GitHub Desktop.
Save kvprasoon/c1bdd4192c3031fd29f5 to your computer and use it in GitHub Desktop.
A oneliner Which uses JSON
#Invoke-WebRequest -Uri www.telize.com/geoip|select -ExpandProperty content|ConvertFrom-Json|select longitude,latitude,country_code,timezone
function Get-GeoInformation
{
[CmdletBinding()]
[Alias('ggeo')]
param(
[Parameter(ValueFromPipeLine=$true)]
[ValidateNotNull()]
[string[]]$IPv4="f")
Begin
{
$output=''
function Check-blank ($Param,[String]$Attrib)
{
Write-Verbose "No Data For Attribute $Attrib"
if($_.latitude -eq $null){"No Data"}else{$Param}
}
}
Process
{
foreach ($IP in $IPv4) {
$IPv4Address=$IP -split "\."
if ($IPv4Address.Count -eq 4 )
{
try
{
$IPv4Result=[System.Net.IPAddress]::TryParse($IP,[ref]"10.10.10.10")
if ($IPv4Result -eq $false)
{
Write-Verbose "Found an Invalid Input for IPAddress"
Write-Error " Cannot validate IPv4 Address"
}
else
{
Write-Verbose "Locating $IP"
Write-Output "Location For $IP"
Invoke-RestMethod -Uri "telize.com/geoip/$IP" |select @{e={Check-blank -Param $_.latitude -Attrib 'Latitude'};l="Latitude"},@{E={Check-blank $_.longitude -Attrib 'Longitude'};L="longitude"},@{E={Check-blank $_.Country_code -Attrib 'Countr_Code'};L='country_code'},@{E={check-blank $_.TimeZone -Attrib 'TimeZone'};L='Timezone'}
}
}
catch
{
throw $_
}
}
elseif($IP -eq "f")
{
Write-Verbose "No Input Found and Locating Default Location"
Invoke-RestMethod -Uri "telize.com/geoip"|select @{e={Check-blank -Param $_.latitude -Attrib 'Latitude'};l="Latitude"},@{E={Check-blank $_.longitude -Attrib 'Longitude'};L="longitude"},@{E={Check-blank $_.Country_code -Attrib 'Countr_Code'};L='country_code'},@{E={check-blank $_.TimeZone -Attrib 'TimeZone'};L='Timezone'}
}
else
{
Write-Verbose "Given Input Found as Not an IPAddress"
Write-Error " Cannot validate IPv4 Address"
}
}
# Write-Output $Info
}
End
{
# $output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment