PowerShell/scripts/list-internet-ip.ps1

37 lines
1.3 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2023-08-22 08:15:53 +02:00
.SYNOPSIS
2024-10-02 10:14:17 +02:00
Lists the Internet IP address
2023-08-22 08:15:53 +02:00
.DESCRIPTION
2024-10-02 10:14:17 +02:00
This PowerShell script queries all public IP address information and prints it.
2023-08-22 08:15:53 +02:00
.EXAMPLE
2024-10-02 10:14:17 +02:00
PS> ./list-internet-ip.ps1
2024-10-01 17:04:39 +02:00
Internet IP 185.72.229.161, 2003:f2:6128:fd01:e543:601:30c2:a028 near Munich, Germany
2023-08-22 08:15:53 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
if ($IsLinux) {
2023-11-27 12:51:01 +01:00
[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)
2023-08-22 08:15:53 +02:00
} else {
2023-11-27 12:51:01 +01:00
[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)
2023-08-22 08:15:53 +02:00
}
2023-09-13 08:41:22 +02:00
if ("$publicIPv4" -eq "") { $publicIPv4 = "no IPv4" }
if ("$publicIPv6" -eq "") { $publicIPv6 = "no IPv6" }
2023-11-27 12:51:01 +01:00
if ("$city" -eq "") { $city = "unknown city" }
if ("$country" -eq "") { $country = "unknown country" }
2024-10-01 17:04:39 +02:00
"✅ Internet IP $publicIPv4, $publicIPv6 near $city, $country"
2023-08-22 08:15:53 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}