IPInfoLite.psm1
### Module Configuration (Private) $script:config = @{ api = @{ baseUrl = "https://api.ipinfo.io/lite/" baseUrlMe = "https://api.ipinfo.io/lite/me" headers = @{ Accept = "application/json" } } } function Get-IPInfoLiteEntry { <# .SYNOPSIS Gets a single IP geolocation and ASN info using IPinfo Lite API. .PARAMETER token Your IPinfo API token. .PARAMETER ip Optional. IP address to look up. If not supplied, looks up caller's IP. .OUTPUTS PSCustomObject with geolocation and ASN information. .EXAMPLE Get-IPInfoLiteEntry -token "your_token_here" -ip "8.8.8.8" Returns geolocation and ASN information for the IP address 8.8.8.8 using the IPInfo Lite API. .EXAMPLE Get-IPInfoLiteEntry -token "your_token_here" Returns geolocation and ASN information for the caller's public IP address using the IPInfo Lite API. #> [CmdletBinding()] [OutputType([PSCustomObject])] param ( [string]$token, [string]$ip = "" ) if ($ip -eq "") { $url = "$($script:config.api.baseUrlMe)?token=$token" } else { $url = "$($script:config.api.baseUrl)$ip" + "?token=$token" } try { $response = Invoke-RestMethod -Uri $url -Method Get -Headers $script:config.api.headers return [PSCustomObject]@{ Success = $true IP = $Response.ip ASN = $Response.asn ASN_Name = $Response.as_name ASN_Domain = $Response.as_domain Country = $Response.country Country_Code = $Response.country_code Continent = $Response.continent Continent_Code = $Response.continent_code } } catch { $json = $_.ErrorDetails.Message | ConvertFrom-Json $errorTitle = $json.error.title $errorMessage = $json.error.message return [PSCustomObject]@{ Success = $false ip = $ip errorTitle = $errorTitle errorMessage = $errorMessage errorRaw = $_.Exception.Message } } } function Get-IPInfoLiteBatch { <# .SYNOPSIS Performs sequential IP info lookups using IPinfo Lite API. .PARAMETER token Your IPinfo API token. .PARAMETER ips Array of IP addresses to look up. .OUTPUTS Array of custom objects with IP info or error messages. .EXAMPLE Get-IPInfoLiteBatch -token "your_token_here" -ips @("8.8.8.8", "1.1.1.1") Performs a batch lookup for multiple IP addresses using the IPInfo Lite API. Returns a list of geolocation and ASN information for each IP. #> [CmdletBinding()] [OutputType([PSCustomObject[]])] param ( [Parameter(Mandatory = $true)] [string]$token, [Parameter(Mandatory = $true)] [string[]]$ips ) # Validate token $testResult = Test-IPInfoLiteToken -token $token if (-not $testResult.Success) { # Optional: Human-readable warning Write-Warning $testResult.Message # Machine-readable structured return return [PSCustomObject]@{ Success = $false Stage = "TokenValidation" Message = $testResult.Message ErrorCode = $testResult.ErrorCode } } $Results = foreach ($ip in $IPs) { $url = $script:config.api.baseUrl + $ip + "?token=" + $token try { $response = Invoke-RestMethod -Uri $url -Method Get -Headers $script:config.api.headers [PSCustomObject]@{ Success = $true IP = $response.ip ASN = $response.asn ASN_Name = $response.as_name ASN_Domain = $response.as_domain Country = $response.country Country_Code = $response.country_code Continent = $response.continent Continent_Code = $response.continent_code } } catch { $json = $_.ErrorDetails.Message | ConvertFrom-Json $errorTitle = $json.error.title $errorMessage = $json.error.message [PSCustomObject]@{ success = $false ip = $ip errorTitle = $errorTitle errorMessage = $errorMessage errorRaw = $_.Exception.Message } } } return $Results } function Test-IPInfoLiteToken { <# .SYNOPSIS Tests the validity of an IPInfo Lite API token. .PARAMETER token Your IPinfo API token. .OUTPUTS Returns a PSCustomObject with these properties: Success [Boolean] Indicates whether the token is valid. Message [String] A descriptive message returned by the API. ErrorCode [Int32] The HTTP status code or custom error code. .EXAMPLE Test-IPInfoLiteToken -token "your_token_here" Tests the validity of the provided IPInfo Lite API token by attempting to query the caller's IP address. Returns $true if the token is valid, $false otherwise. #> [CmdletBinding()] [OutputType([PSCustomObject])] param ( [Parameter(Mandatory)] [string]$token ) $url = "$($script:config.api.baseUrlMe)?token=$token" try { $null = Invoke-RestMethod -Uri $url -Headers $script:config.api.headers -Method Get -TimeoutSec 5 return [PSCustomObject]@{ Success = $true Message = "Token is valid." } } catch { return [PSCustomObject]@{ Success = $false Message = "Token validation failed: $($_.Exception.Message)" ErrorCode = $_.Exception.Response.StatusCode.Value__ } } } Export-ModuleMember -Function Get-IPInfoLiteEntry, Get-IPInfoLiteBatch |