Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# One-liner, command and parameter names spelled out
Invoke-RestMethod -Uri 'www.telize.com/geoip' | Format-Table -Property longitude, latitude, continent_code, timezone
# Advanced function wrapping the www.telize.com JSON endpoint
function Get-GeoInformation {
<#
.SYNOPSIS
Get IP address geo information
.DESCRIPTION
Get IP address geo information from the www.telize.com/geoip JSON endpoint
.EXAMPLE
Get-GeoInformation
Get geo information for the visitor IP address using default protocol (HTTPs)
.EXAMPLE
Get-GeoInformation -Protocol HTTP
Get geo information for the visitor IP address using HTTP protocol
.EXAMPLE
Get-GeoInformation -IPAdress '83.94.121.217', '83.94.121.251'
Get geo information for specified IP addresses
.EXAMPLE
'83.94.121.217', '83.94.121.251' | Get-GeoInformation -Verbose
Get geo information for IP addresses from pipeline
#>
[CmdletBinding()]
param(
# IP Address(es) to get geo data for
[Parameter(Position = 0, ValueFromPipeline = $true)]
[ValidateScript({ [IPAddress]$_ })]
[String[]]
$IPAddress = ''
,
# Protocol used for the request (default: HTTPS)
[Parameter(Position = 1)]
[ValidateSet('HTTPS','HTTP')]
[String]
$Protocol = 'HTTPS'
)
BEGIN {
Write-Verbose "Using protocol: $($Protocol.ToUpper())"
$Uri = "$($Protocol.ToLower())://www.telize.com/geoip"
Write-Verbose "Using JSON endpoint: $Uri"
}
PROCESS {
foreach ($IP in $IPAddress) {
# If $IP is blank, lookup visitor IP, else specified IP
Switch ($IP) {
'' {
Write-Verbose 'Looking up geo data for visitor IP'
$LookupUri = $Uri
}
default {
Write-Verbose "Looking up geo data for specified IP: $IP"
$LookupUri = "$Uri/$IP"
}
}
Try {
$RestObject = Invoke-RestMethod -Uri $LookupUri -ErrorAction Stop
$Reply = $true
$ErrorMessage = ''
}
Catch {
$Error[0]
$Reply = $false
$ErrorMessage = $Error[0]
}
$Properties = [Ordered]@{
'Uri' = $LookupUri
'Reply' = $Reply
'ErrorMessage' = $ErrorMessage
'ip' = $RestObject.ip
'country_code' = $RestObject.country_code
'country_code3' = $RestObject.country_code3
'country' = $RestObject.country
'region_code' = $RestObject.region_code
'region' = $RestObject.region
'city' = $RestObject.city
'postal_code' = $RestObject.postal_code
'continent_code' = $RestObject.continent_code
'latitude' = $RestObject.latitude
'longitude' = $RestObject.longitude
'dma_code' = $RestObject.dma_code
'area_code' = $RestObject.area_code
'asn' = $RestObject.asn
'isp' = $RestObject.isp
'timezone' = $RestObject.timezone
}
New-Object -TypeName PSObject -Property $Properties
}
}
}
# Another interesting service:
# HP Instant Support Enterprise Edition (accepts SOAP requests)
# https://services.isee.hp.com/EntitlementCheck/EntitlementCheckService.asmx
# https://services.isee.hp.com/ClientRegistration/ClientRegistrationService.asmx
#
# Documentation is not publicly available, making it hard to work with, but
# fortunately dotps1 already made a good starting point:
# https://github.com/dotps1/HPWarranty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment