Public/Network/Get-IPInfo.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Get-IPInfo {
    <#
    .SYNOPSIS
    Retrieves geographic information.
 
    .DESCRIPTION
    Retrieves geographic information about .
 
    .PARAMETER None
 
    .EXAMPLE
    Get-GeoInfo
 
    .NOTES
    The function uses a web API to cpllect geographic data.
    #>

    
    [CmdletBinding()]
    PARAM (
        [System.Net.IPAddress[]]$IPAddress = "127.0.0.1"
    )
    
    $Results = @()

    ### IP and Geo Location Data
    FOREACH ($IP in $IPAddress) {
        IF ($IP -eq "127.0.0.1") { 
            $GeoData = Invoke-RestMethod -Method Get -Uri "http://ip-api.com/json/$null" 
            
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                IP = $GeoData.Query
                IPClass = "Public"
                Country = $GeoData.Country
                State = $GeoData.RegionName
                City = $GeoData.City
                ZipCode = $GeoData.Zip
                Org = $GeoData.Org
                ISP = $GeoData.ISP
            }        
        }
        ELSEIF ((Test-IsPublicIP $IP)) {
            $GeoData = Invoke-RestMethod -Method Get -Uri "http://ip-api.com/json/$IP" 
            
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                IP = $IP
                IPClass = "Public"
                Country = $GeoData.Country
                State = $GeoData.RegionName
                City = $GeoData.City
                ZipCode = $GeoData.Zip
                Org = $GeoData.Org
                ISP = $GeoData.ISP
            }
        }
        ELSE {
            Write-Verbose "Only possible to lookup geodata for public IP addresses."
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                IP = $IP
                IPClass = "Private"
                Country = $null
                State = $null
                City = $null
                ZipCode = $null
                Org = $null
                ISP = $null
            }
        }
        
    }

    RETURN $Results | Select IP, IPClass, Country, State, City, ZipCode, Org, ISP
}
New-Alias -Name Get-GeoInfo -Value Get-IPInfo