PowerShell/scripts/list-public-ip.ps1

34 lines
1.1 KiB
PowerShell
Raw Normal View History

2023-10-31 12:33:36 +01:00
<#
2023-08-22 08:15:53 +02:00
.SYNOPSIS
Lists the public IP address(es)
.DESCRIPTION
This PowerShell script queries the public IP address(es) and prints it.
.EXAMPLE
PS> ./list-public-ip.ps1
2023-09-13 08:41:22 +02:00
Public IP address 185.72.209.161, 2003:f2:6128:fc01:e503:601:30c2:a028 near Munich
2023-08-22 08:15:53 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
if ($IsLinux) {
$publicIPv4 = (curl -4 --silent ifconfig.co)
$publicIPv6 = (curl -6 --silent ifconfig.co)
$city = (curl --silent ifconfig.co/city)
} else {
$publicIPv4 = (curl.exe -4 --silent ifconfig.co)
$publicIPv6 = (curl.exe -6 --silent ifconfig.co)
$city = (curl.exe --silent ifconfig.co/city)
}
2023-09-13 08:41:22 +02:00
if ("$publicIPv4" -eq "") { $publicIPv4 = "no IPv4" }
if ("$publicIPv6" -eq "") { $publicIPv6 = "no IPv6" }
2023-08-22 08:15:53 +02:00
if ("$city" -eq "") { $city = "unknown" }
2023-09-13 08:41:22 +02:00
Write-Output "✅ Public IP address $publicIPv4, $publicIPv6 near $City"
2023-08-22 08:15:53 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}