Powershell and External IP Addresses

If you're looking for help with C#, .NET, Azure, Architecture, or would simply value an independent opinion then please get in touch here or over on Twitter.

While doing work with Powershell and Azure Resource Manager templates today I wanted to configure the Azure SQL Database firewall to allow my build machine access to configure schema and stored procedures. To reliably know what your Internet facing IP one fairly reliable method is to bounce it back from a remote server and so I stood up a super simple website on Azure with a REST API to return to you your IP. You can find it’s swagger endpoint here. All that’s really behind the get action is a simple return of a request header:

public string Get()
{
    return HttpContext.Current.Request.UserHostAddress;
}

Getting hold of it from Powershell is pretty simple, you can use the Invoke-WebRequest cmdlet as follows (and I’m also removing the double quotes surrounding the returned json string):

$ipAddress = (Invoke-WebRequest https://afwhatsmyip.azurewebsites.net/ip).Content
$ipAddress = $ipAddress.Substring(1,$ipAddress.Length-2)

Feel free to use the endpoint yourself, I’m not planning on taking it down.

Leave a Reply

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

Contact

  • If you're looking for help with C#, .NET, Azure, Architecture, or would simply value an independent opinion then please get in touch here or over on Twitter.

Recent Posts

Recent Tweets

Recent Comments

Archives

Categories

Meta

GiottoPress by Enrique Chavez