SmartPlanets.AzureUtilities.psm1

Import-Module AzureRM

<#
 .Synopsis
  Adds the current host's IP to an Azure SQL Server firewall rule list.
 
 .Description
  Adds the current host's IP to an Azure SQL Server firewall rule list.
 
 .Parameter AzureSubscription
  The Azure subscription that holds the requested Azure SQL Server.
 
 .Parameter SqlServerName
  The name of the Azure SQL Server.
 
 .Parameter RuleName
  An optional name for the rule. By default, it takes the host name as defined in Windows.
 
 .Example
   # Add a rule to an Azure SQL Server named contoso, located in the "Pay-As-You-Go" Azure subcription.
   Register-SqlServerHostFirewallRule -AzureSubscription "Pay-As-You-Go" -SqlServerName "contoso"
 
 .Example
   # Add a rule to an Azure SQL Server named contoso, located in the "Test" Azure subcription, with a custom rule name.
   Register-SqlServerHostFirewallRule -AzureSubscription "Test" -SqlServerName "contoso" -RuleName "John's PC"
#>

function Register-SqlServerHostFirewallRule {
    param(
        [Parameter(Mandatory = $true)]
        [string]$AzureSubscription,
        [Parameter(Mandatory = $true)]
        [string]$SqlServerName,
        [Parameter(Mandatory = $false)]
        [string]$RuleName
    )

    $ErrorActionPreference = 'Stop'
    $InformationPreference = 'Continue'

    Write-Information "Getting public IP using ipconfig.me..."
    $publicIP = (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content

    if($publicIP)
    {
        Write-Information "Your public IP is $publicIP."
    }
    else
    {
        Write-Error "Could not get public IP from API. Check your internet connection, or check that http://ifconfig.me/ip is online."
    }

    if(!$RuleName)
    {
        $RuleName = $env:computername
    }

    Write-Information "Please provide your Azure credentials."
    Connect-AzureRmAccount -Subscription $AzureSubscription | Out-Null

    Write-Information "Connected. Searching for the requested SQL Server..."
    $server = Get-AzureRmResource | Where-Object {$_.ResourceType –eq "Microsoft.Sql/servers" -and $_.Name -eq $SqlServerName}
    if($server)
    {
        Write-Information "SQL Server found. Searching for existing firewall rule..."
        $currentRule = Get-AzureRmSqlServerFirewallRule -FirewallRuleName $RuleName -ServerName $server.ResourceName -ResourceGroupName $server.ResourceGroupName -ErrorAction SilentlyContinue
    
        if($currentRule)
        {
            Write-Information "Deleting existing rule to update with current IP..."
            $oldRule = Remove-AzureRmSqlServerFirewallRule -FirewallRuleName $RuleName -ServerName $server.ResourceName -ResourceGroupName $server.ResourceGroupName

            if($oldRule)
            {
                Write-Information "The rule was successfully deleted."
            }
            else
            {
                Write-Error "The rule was not deleted. Please check the Azure Portal."
                Disconnect-AzureRmAccount
            }
        }
        else
        {
            Write-Information "A rule named $RuleName does not exist. Creating new rule..."
        }

        $newRule = New-AzureRmSqlServerFirewallRule -FirewallRuleName $RuleName -ServerName $server.ResourceName -ResourceGroupName $server.ResourceGroupName -StartIpAddress $publicIP -EndIpAddress $publicIP

        if($newRule)
        {
            Write-Information "The new rule was successfully created."
        }
        else
        {
            Write-Error "The new rule was not created. Please check the Azure Portal."
            Disconnect-AzureRmAccount
        }
    }
    else
    {
        Write-Error "The requested SQL Server does not exist in the provided subscription. Check both the server and subscription names and try again."
        Disconnect-AzureRmAccount
    }

    Disconnect-AzureRmAccount | Out-Null
}
Export-ModuleMember -Function Register-SqlServerHostFirewallRule