PowerShell/docs/list-public-ip.md

74 lines
2.3 KiB
Markdown
Raw Normal View History

2023-09-01 17:53:03 +02:00
*list-public-ip.ps1*
================
2023-12-07 20:24:45 +01:00
This PowerShell script queries the public IP address information and prints it.
2023-09-01 17:53:03 +02:00
Parameters
----------
```powershell
PS> ./list-public-ip.ps1 [<CommonParameters>]
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
PS> ./list-public-ip.ps1
2023-12-07 20:24:45 +01:00
✅ Public IP address 185.72.229.161, 2003:f2:6128:fc01:e543:601:30c2:a028 near Munich, Germany
2023-09-01 17:53:03 +02:00
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
2023-12-07 20:24:45 +01:00
Lists the public IP address information
2023-09-01 17:53:03 +02:00
.DESCRIPTION
2023-12-07 20:24:45 +01:00
This PowerShell script queries the public IP address information and prints it.
2023-09-01 17:53:03 +02:00
.EXAMPLE
PS> ./list-public-ip.ps1
2023-12-07 20:24:45 +01:00
✅ Public IP address 185.72.229.161, 2003:f2:6128:fc01:e543:601:30c2:a028 near Munich, Germany
2023-09-01 17:53:03 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
if ($IsLinux) {
2023-12-07 20:24:45 +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-09-01 17:53:03 +02:00
} else {
2023-12-07 20:24:45 +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-09-01 17:53:03 +02:00
}
2023-09-13 09:49:05 +02:00
if ("$publicIPv4" -eq "") { $publicIPv4 = "no IPv4" }
if ("$publicIPv6" -eq "") { $publicIPv6 = "no IPv6" }
2023-12-07 20:24:45 +01:00
if ("$city" -eq "") { $city = "unknown city" }
if ("$country" -eq "") { $country = "unknown country" }
Write-Output "✅ Public IP address $publicIPv4, $publicIPv6 near $city, $country" ; if ($country -eq "Russia") { Write-Host -foregroundColor red "DON'T TRUST PUTIN !!! FLEE NOW OR DIE AS SOLDIER IN UKRAINE !!!" }
2023-09-01 17:53:03 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-01-25 13:31:10 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of list-public-ip.ps1 as of 01/25/2024 13:28:36)*