Find your IPv4 and IPv6 addresses

My first 100% AI generated blog post

Who is that hooded dude? (click to enlarge)

I asked GitHub Copilot to explain a simple PowerShell function…and it did better than I could. The explanation follows verbatim along with the code below. Also, check out the image to the left. Dall-E is definitely hallucinating.

This PowerShell script defines a function named Get-MyIpAddress that fetches and displays the IPv4 and IPv6 addresses of the machine where the script is run.

The function starts by using the Invoke-WebRequest cmdlet to send a web request to https://ipv4.seeip.org. This website returns the IPv4 address of the client making the request. The .Content property is used to extract the body of the response, which is the IPv4 address. The -join operator is then used to concatenate this address with “/32”, which is the subnet mask for a single IPv4 address. This result is stored in the $ipv4 variable.

Next, the function does the same for the IPv6 address. It sends a request to https://ipv6.seeip.org, which returns the IPv6 address of the client. The address is then concatenated with “/128”, which is the subnet mask for a single IPv6 address. This result is stored in the $ipv6 variable.

The function then creates a string that contains both the IPv4 and IPv6 addresses. The string is formatted to display the protocol type (IPv4 or IPv6), followed by a tab (`t), the corresponding IP address, and a newline (`n). This string is stored in the $output variable.

Finally, the function uses the Write-Host cmdlet to print the $output string to the console. The -ForegroundColor Yellow parameter is used to change the color of the text to yellow. This makes the output of the function easy to read in the console.

function Get-MyIpAddress {
    $ipv4 = -join $((Invoke-WebRequest "https://ipv4.seeip.org").Content, "/32")
    $ipv6 = -join $((Invoke-WebRequest "https://ipv6.seeip.org").Content, "/128")
    $output = "Protocol:IPv6`t$ipv6`nProtocol:IPv4`t$ipv4"
    Write-Host -ForegroundColor Yellow -Object $output
}

Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *