Created
January 28, 2022 19:08
-
-
Save mwjcomputing/ab4bd849d50061df70e3bc4cb010f4fc to your computer and use it in GitHub Desktop.
Get a host's public IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-PublicIP { | |
param ( | |
[Parameter(HelpMessage='Return IPv6 info if available.')] | |
[switch] $ipV6 | |
) | |
if ($ipV6) | |
{ | |
$ipifyURL = 'https://api64.ipify.org' | |
} | |
else | |
{ | |
$ipifyURL = 'https://api.ipify.org' | |
} | |
try | |
{ | |
$publicIP = (Invoke-WebRequest -Uri $ipifyURL).Content | |
} | |
catch [WebException] | |
{ | |
Write-Host 'There was a problem using the api to retrieve your client IP.' | |
} | |
$publicIP | |
<# | |
.SYNOPSIS | |
Returns public IP for host. | |
.DESCRIPTION | |
Returns public IP for host using ipify.org. | |
.EXAMPLE | |
PS C:\> Get-PublicIP | |
Gets current public IP. | |
.EXAMPLE | |
PS C:\> Get-PublicIP -ipV6 | |
Gets current public IPv6 IP if available. | |
.INPUTS | |
System.Boolean. If the IPv6 info should be returned. | |
.OUTPUTS | |
System.String. The public IP. | |
.NOTES | |
Written by Matthew Johnson (@mwjcomputing) | |
.LINK | |
https://mwjcomputing.com | |
.LINK | |
https://gist.github.com/mwjcomputing/ab4bd849d50061df70e3bc4cb010f4fc | |
#> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment