A common task-within-a-task for Sysadmins is locating the Public-facing IP Address for a given Windows Server.
This may be done for the purposes of testing a new web app, or for the configuration of public DNS records.
Whatever the reason, here is an easy way to obtain that information without using a browser (especially since many sites which claim to be for public IP address identification are packed with ads, scripts, and other elements a wise System Administrator would avoid accessing from a server, especially while logged in with administrative credentials.
First, we assign a target URL to a variable called “$zurl”. The URL provided returns the data we need when called.
Now we have our URL stored as a string variable. You can always test the contents of a string variable by simply typing the variable name and pressing ENTER.
Now that we have the URL as a variable, we need a way to download data. This time we will use the NET.Webclient library from Microsoft. It’s already built into Windows, so let’s assign it to a variable as follows.
We now have both our target URL and our web client as variables within system memory, ready for use. Let’s proceed to getting the Public IP value.
Oh look. We got an SSL error. If you have been following this series carefully, you already know how to address this but, of course, we will cover it again here.
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
That should take care of the TLS 1.2 incompatibility which is the PowerShell (and Internet Explorer) default on most Windows Servers today. Let’s rerun the prior line to see the difference.
Alright, we have our Public IP address data stored now, in a variable called $zpublic, which could then be used for other operations or apps, should you desire. An example may be to programmatically update a DNS record, or to programmatically update this value in a database.
Clearly I was not going to show you the public IP of my system (what kind of IT Security Professional would I be if I did…), but what is shown is sufficient for you to retrieve your own Public IP address values, using a public-facing service that Amerihub provides (at this time) for free.
Now please, go forth and use this newfound knowledge (which is Power, they say) for Good instead of for Evil.
And if you found this valuable, or if you find any of these tutorials valuable for your career or your business, donate here: