I need my external IP in my PowerShell

Problem:

I need my external IP address so I can set an Azure SQL Database Server Firewall Rule.

Solution:

Write an ASP.NET Web API you can host in a free Azure Website that will simply return the IP of the caller. 

Explanation:

I was recently working on a PowerShell script so I could provision an Azure SQL Database Server during my deployment using Release Management.  Creating the server is actually pretty easy, however, to create the database I had to create a firewall rule first.  The Azure PowerShell has a cmdlet to get and add Azure SQL Firewall rules.  The problem was I needed to know the IP Address of the machine I was executing the commands from.  When it is your own machine it is pretty easy to find unless you are behind a router.  When you are behind a router a simple call to ipconfig will only show you the IP assigned to you by the router which is not the IP seen by Azure.  This gets even more complicated when it is an agent machine potentially hosted anywhere.

With the Web API deployed into a free Azure Website I make a simple PowerShell call to the Web API to return my IP.

#get my external ip address

Write-Verbose "Getting agent IP Address"

$wc = new-object System.Net.WebClient

$myip = $wc.DownloadString("http://[site].azurewebsites.net/api/ip").Replace("""", "")


With my IP in hand I can now make the call to the New-AzureSqlDatabaseServerFirewallRule cmdlet.

Write-Verbose "Trying to locate firewall rule for IP Address $myip"

if((Get-AzureSqlDatabaseServerFirewallRule -ServerName $serverName -WarningAction SilentlyContinue |  Where-Object {$_.StartIpAddress -eq $myip -or $_.EndIpAddress -eq $myip}) -eq $null)

{

   Write-Verbose "Adding firewall rule for IP address $myip"

   New-AzureSqlDatabaseServerFirewallRule -ServerName $server.ServerName -RuleName "AgentAccess" -StartIpAddress $myip -EndIpAddress $myip | Out-Null

}

else

{

   Write-Verbose "Firewall rule for IP address $myip already exists"

}

 

You have to make sure you first check if the rule already exists or you will get an error when you attempt to create a duplicate rule.

The code for the Web API is extremely simple.

namespace WhatIsMyIP.Controllers

{

   #region using

 

   using System.Web.Http;

 

   #endregion

 

   public class IPController : ApiController

   {

      #region Public Methods and Operators

 

      public string Get()

      {

         if (this.Request.Properties.ContainsKey("MS_HttpContext"))

         {

            dynamic ctx = this.Request.Properties["MS_HttpContext"];

            if (ctx != null)

            {

               return ctx.Request.UserHostAddress;

            }

         }

         return null;

      }

 

      #endregion

   }

}

You can download the project here:

WhatIsMyIP.zip (3.9MB)

Comments (1) -

  • Ferry

    6/1/2018 8:41:06 AM | Reply

    I'm not aware this existed when you wrote the article, but I guess this problem would have been solved by now.

    https://www.ipify.org/

Add comment

Loading