Last active
January 22, 2023 17:08
-
-
Save Timberfang/8f37a10da7c0d2d9a785ed8964bde3c4 to your computer and use it in GitHub Desktop.
This file contains 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
<# | |
.SYNOPSIS | |
Get the public and local IP addresses of the current computer. | |
.DESCRIPTION | |
Get the public and local IP addresses for the computer running the command. The local address can be retreived at any time, | |
as long as the computer is connected to a network. The public address depends on the website ident.me for retrieving the address. | |
If this website is down, the public address will not be displayed. | |
.EXAMPLE | |
PS C:\>.\Get-IPAddress.ps1 | |
LocalIP PublicIP | |
------- -------- | |
192.0.2.0 203.0.113.0 | |
#> | |
$LocalIP = Get-NetIPAddress -PrefixOrigin DHCP -ErrorAction SilentlyContinue | |
$IPAddress = [PSCustomObject]@{ | |
LocalIP = | |
if ($null -ne $LocalIP) {$LocalIP} | |
else { 'None' } | |
PublicIP = | |
if (Test-Connection -Ping example.com -Quiet -Count 1) { Invoke-WebRequest -Uri 'https://ident.me/' } | |
else { 'None' } | |
} | |
Write-Output $IPAddress |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment