PowerShell/docs/list-internet-ip.md
2024-11-20 11:52:20 +01:00

2.1 KiB

The list-internet-ip.ps1 Script

This PowerShell script queries all public IP address information and prints it.

Parameters

/home/markus/Repos/PowerShell/scripts/list-internet-ip.ps1 [<CommonParameters>]

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./list-internet-ip.ps1
 Internet IP 185.72.229.161, 2003:f2:6128:fd01:e543:601:30c2:a028 near Munich, Germany

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
        Lists the Internet IP address
.DESCRIPTION
        This PowerShell script queries all public IP address information and prints it.
.EXAMPLE
        PS> ./list-internet-ip.ps1
	✅ Internet IP 185.72.229.161, 2003:f2:6128:fd01:e543:601:30c2:a028 near Munich, Germany
.LINK
        https://github.com/fleschutz/PowerShell
.NOTES
        Author: Markus Fleschutz | License: CC0
#>

try {
	if ($IsLinux) {
		[string]$publicIPv4 = (curl -4 --silent ifconfig.co)
		[string]$publicIPv6 = (curl -6 --silent ifconfig.co)
		[string]$city = (curl --silent ifconfig.co/city)
		[string]$country = (curl --silent ifconfig.co/country)
	} else {
		[string]$publicIPv4 = (curl.exe -4 --silent ifconfig.co)
		[string]$publicIPv6 = (curl.exe -6 --silent ifconfig.co)
		[string]$city = (curl.exe --silent ifconfig.co/city)
		[string]$country = (curl.exe --silent ifconfig.co/country)
	}
	if ("$publicIPv4" -eq "") { $publicIPv4 = "no IPv4" }
	if ("$publicIPv6" -eq "") { $publicIPv6 = "no IPv6" }
	if ("$city" -eq "")       { $city = "unknown city" }
	if ("$country" -eq "")    { $country = "unknown country" }
	"✅ Internet IP $publicIPv4, $publicIPv6 near $city, $country"
	exit 0 # success
} catch {
        "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
        exit 1
}

(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:56)