PowerShell Requires Chilkat v11.0.0+
PowerShell
Look up an IP location with IPinfo Core
See more Geolocation Examples
Use an IPinfo token with Core, Plus, or Max access. The current api.ipinfo.io/lookup/ endpoint returns location fields inside geo and network fields inside as. Authenticate with a Bearer token.
IPinfo continues to support its older ipinfo.io API for existing integrations. This example uses the current Core response structure. The free Lite API uses /lite/ and a different, country-level response. Choose the endpoint and parser for your subscription.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Use a public IP address. Provider results can change as their databases are updated.
$ipAddress = "8.8.8.8"
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.ConnectTimeout = 10
$http.ReadTimeout = 30
$http.Accept = "application/json"
$http.AuthToken = "IPINFO_TOKEN"
$url = New-Object Chilkat.StringBuilder
$url.Append("https://api.ipinfo.io/lookup/")
$url.Append($http.UrlEncode($ipAddress))
$response = New-Object Chilkat.HttpResponse
$success = $http.HttpNoBody("GET",$url.GetAsString(),$response)
if ($response.StatusCode -eq 0) {
# Request diagnostics can contain an API key. Report transport failure without logging credentials.
$("No HTTP response. Check connectivity, DNS, and TLS configuration.")
exit
}
$json = New-Object Chilkat.JsonObject
$parsed = $json.Load($response.BodyStr)
if (($success -ne $true) -or ($response.StatusCode -lt 200) -or ($response.StatusCode -ge 300)) {
$("Lookup failed. HTTP " + $response.StatusCode)
if ($parsed -eq $true) {
if ($json.JsonTypeOf("error.title") -eq 1) {
$($json.StringOf("error.title"))
}
if ($json.JsonTypeOf("error.message") -eq 1) {
$($json.StringOf("error.message"))
}
}
exit
}
if ($parsed -ne $true) {
$("Expected a JSON object in the lookup response.")
exit
}
if (($json.JsonTypeOf("error") -eq 3)) {
$("The service could not complete the lookup.")
if ($json.JsonTypeOf("error.title") -eq 1) {
$($json.StringOf("error.title"))
}
if ($json.JsonTypeOf("error.message") -eq 1) {
$($json.StringOf("error.message"))
}
exit
}
if (($json.JsonTypeOf("bogon") -eq 5) -and ($json.BoolOf("bogon") -eq $true)) {
$("This is a reserved or otherwise non-public IP address.")
exit
}
if (($json.JsonTypeOf("ip") -ne 1) -or ($json.StringOfEquals("ip","",$true) -eq $true)) {
$("The response did not contain the expected IP address.")
exit
}
$("IP address: " + $json.StringOf("ip"))
if (($json.JsonTypeOf("geo.country") -eq 1) -and ($json.StringOfEquals("geo.country","",$true) -ne $true)) {
$("Country: " + $json.StringOf("geo.country"))
}
else {
$("Country: unavailable")
}
if (($json.JsonTypeOf("geo.country_code") -eq 1) -and ($json.StringOfEquals("geo.country_code","",$true) -ne $true)) {
$("Country code: " + $json.StringOf("geo.country_code"))
}
else {
$("Country code: unavailable")
}
if (($json.JsonTypeOf("geo.region") -eq 1) -and ($json.StringOfEquals("geo.region","",$true) -ne $true)) {
$("Region: " + $json.StringOf("geo.region"))
}
else {
$("Region: unavailable")
}
if (($json.JsonTypeOf("geo.city") -eq 1) -and ($json.StringOfEquals("geo.city","",$true) -ne $true)) {
$("City: " + $json.StringOf("geo.city"))
}
else {
$("City: unavailable")
}
if (($json.JsonTypeOf("geo.timezone") -eq 1) -and ($json.StringOfEquals("geo.timezone","",$true) -ne $true)) {
$("Time zone: " + $json.StringOf("geo.timezone"))
}
else {
$("Time zone: unavailable")
}
# Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (($json.JsonTypeOf("geo.latitude") -eq 2) -and ($json.JsonTypeOf("geo.longitude") -eq 2)) {
$("Coordinates: " + $json.StringOf("geo.latitude") + ", " + $json.StringOf("geo.longitude"))
}
else {
$("Coordinates: unavailable")
}
if (($json.JsonTypeOf("as.asn") -eq 1) -and ($json.StringOfEquals("as.asn","",$true) -ne $true)) {
$("ASN: " + $json.StringOf("as.asn"))
}
else {
$("ASN: unavailable")
}
if (($json.JsonTypeOf("as.name") -eq 1) -and ($json.StringOfEquals("as.name","",$true) -ne $true)) {
$("Network organization: " + $json.StringOf("as.name"))
}
else {
$("Network organization: unavailable")
}