Private/IPAX-IPGeoLocation.ps1

$dotnetclass = @"
public class GEOInformation
{
   public GEOInformation()
    {
         
    }
 
    public string Country { get; set; }
    public string CountryCode { get; set; }
    public string Region { get; set; }
    public string RegionCode { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string TimeZone { get; set; }
    public string Provider { get; set; }
    public string Organization { get; set; }
    public string Handle { get; set; }
    public string PTR { get; set; }
 
    public override string ToString()
    {
        return Organization + ", " + PostalCode + " " + City + ", " + Region + ", " + Country ;
    }
}
"@

Add-Type -TypeDefinition $dotnetclass

function Get-IPAXGeoInformation {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [ValidatePattern('((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}')]
        [string]$IPAddress
    )
    $uri = ('http://ip-api.com/json/{0}?fields=status,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,reverse' -f $IPAddress)
    try {
        $response = Invoke-RestMethod -Method Get -Uri $uri
    } catch {
        Write-Warning "Error connecting to '$uri': $($_.Exception.Message)"
        return $null
    }
    if ($response.status -eq 'success') {
        $result = [GEOInformation]::new()
        $result.Country = $response.country
        $result.CountryCode = $response.countryCode
        $result.Region = $response.regionName
        $result.RegionCode = $response.region
        $result.City = $response.city
        $result.PostalCode = $response.zip
        $result.TimeZone = $response.timezone
        $result.Provider = $response.isp
        $result.Organization = $response.org
        $result.Handle = $response.as
        $result.PTR = $response.reverse
    } else {
        Write-Warning "GEOinformation retrieval unsuccessfull: $response"
        $result = $null
    }
    return $result
}