Get-WANIPInfo.psm1
<#
.SYNOPSIS Get-WANIPInfo - Lookup a specific IP (If none specified, current WAN IP used) .DESCRIPTION Get-WANIPInfo - Lookup a specific IP (If none specified, current WAN IP used) .NOTES Authors : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk Version : 1.1.0 Date : 2023.06.23 Update : 1.1.0 SG - 2023.06.23 Added UseBasicParsing switch Update : 1.0.9 SG - 2022.08.30 Excluded IPv6 addresses returned when using DnsName, First IPv4 address will be used if multiple returned Update : 1.0.8 SG - 2022.04.25 Fixed Author details Update : 1.0.7 SG - 2022.04.24 Updated Company Name Update : 1.0.6 SG - 2022.04.24 Created Module Comment-Based Help .LINK http://www.FocusedIT.co.uk #> function Get-WANIPInfo{ <# .SYNOPSIS Get-WANIPInfo .DESCRIPTION Get-WANIPInfo - Lookup a specific IP (If none specified, current WAN IP used) .EXAMPLE Get-WANIPInfo .EXAMPLE Get-WANIPInfo 1.1.1.1 .EXAMPLE Get-WANIPInfo www.google.com -Token ipinfo.io_API_Token .NOTES Authors : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk Version : 1.0.7 Date : 2023.06.23 Update : 1.0.7 SG - 2023.06.23 Added UseBasicParsing switch Update : 1.0.6 Excluded IPv6 addresses returned when using DnsName First IPv4 address will be used if multiple returned Update : 1.0.5 SG - 2022.04.24 Added Examples Update : 1.0.4 SG - 2022.04.24 Added Token Parameter for ipinfo.io API Added Hostname Alias for IP Parameter Set default Parameter Value to Look up at ifconfig.me Update : 1.0.3 SG - 2021.08.13 Updated to retrieve External WAN IP automatically if not specified Update : 1.0.2 SG - 2021.01.22 Added Support for Hostnames Update : 1.0.1 SG - 2017.05.12 Parameter Mandatory=$false Update : 1.0.0 SG - 2017.05.12 Initial Script - Copied from New-WANIPLookup (1.0.0) .LINK http://www.FocusedIT.co.uk #> [CmdletBinding()] Param( [Parameter(Position=0,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [alias('Hostname')] [string]$IP = (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content, [Parameter(Position=1,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$false)] [string]$Token , [switch]$UseBasicParsing #, ) if(!("$IP" -as [ipaddress])){ $Hostname = $IP try{ if(Resolve-DnsName $hostname -DnsOnly -Type A -ErrorAction SilentlyContinue){ $IP = (Resolve-DnsName $hostname -DnsOnly -Type A -ErrorAction SilentlyContinue).IPAddress if($IP.count -gt 1){ $IP = $IP[0] Write-Warning "Multiple IPs detected, using first found: $IP" } } else { throw "$hostname is not valid" } } catch { Write-Error $Global:Error[0] -ErrorAction Stop } } Write-Host "Results: " -NoNewline Write-Host $IP -ForegroundColor Magenta -NoNewline if($hostname){ Write-Host " ($hostname)" -ForegroundColor Magenta -NoNewline } Write-Host " at http://ipinfo.io" $address = "http://ipinfo.io/"+$IP+'?token='+$Token if($UseBasicParsing){ Invoke-RestMethod $address -UseBasicParsing }else{ Invoke-RestMethod $address } } |