Windows PowerShell – comes in handy in roaming scenarios (with a direct Internet connection) where your public IP address is assigned dynamically, forcing you to modify your firewall rules on a regular basis. To implement this solution, install and import Windows Azure Module for Windows PowerShell(its setup program is available from Azure Downloads, but note that its execution triggers Web Platform Installer that automatically downloads and installs all necessary prerequisites, including Windows Azure SDK and .NET Framework 4.5); connect to your subscription, and run the New-AzureSqlDatabaseServerFirewallRule (or Set-AzureSqlDatabaseServerFirewallRuleif modifying an existing firewall rule) cmdlet. The first of these steps is quite straightforward, so let’s focus on the remaining two.
In general, there are two ways of connecting to your subscription. The first one relies on the account-based authentication and involves logging on to Windows Azure by explicitly specifying your credentials (in response to the Add-AzureAccount cmdlet that you launch). You can use either a Microsoft Account or an Organizational ID for this purpose, as long as the one you choose is designated as either the Azure Service Administrator or a Co-administrators for the target subscription. (You can manage these assignments via the Administrators tab of the Settings page within the management portal). An alternative approach relies on certificate-based authentication and involves generating and importing a management certificate that represents your subscription with corresponding credentials. This is accomplished by invoking the Get-AzurePublishSettingsFile cmdlet, which allows you to download from Azure a .publishsettings file and, subsequently, placing its content in your Windows user profile (specifically, in the C:UsersUser_NameAppDataRoamingWindows Azure Powershell folder and your personal certificate store) by running the Import-AzurePublishSettingsFilecmdlet.
With the first of these two methods, credentials are cached for 12 hours, which makes it ill suited for scenarios that involve execution of repetitive, scheduled tasks. While the second approach eliminates this shortcoming (providing continuous access as long as the subscription and management certificate remain valid), it introduces security and manageability challenges (especially when providing shared access to a subscription or when dealing with multiple subscriptions by using the same account).
These two procedures translate into the following PowerShell code sequence:
1. Connect to Windows Azure.
1a. Either use explicit credentials (this will launch a separate browser window prompting for your username and password),
Add-AzureAccount
1b. or generate management certificate, download the .publishsettings file, and import it into your profile.
Get-AzurePublishSettingsFile
Import-AzurePublishSettingsFile
2. List your Azure subscriptions (optional) in order to identify the SubscriptionName that hosts the target SQL Server instance.
Get-AzureSubscription
3. Connect to the target subscription (note that the credentials you specified must be associated with this subscription).
Select-AzureSubscription "SubscriptionName"
4. List your SQL Server instances in order to identify the ServerName that hosts the target SQL Database (optional).
Get-AzureSqlDatabaseServer
5. List existing firewall rules on the target SQL Server-level (optional).
Get-AzureSqlDatabaseServerFirewallRule -ServerName ServerName | ft -auto
6. Identify your current IP address based on the method published in the TechNet Script Center (this assumes that you have a direct Internet connection).
$currentIP = (New-Object net.webclient).downloadstring("http://checkip.dyndns.com") -replace "[^d.]"
7. Check if there is an existing firewall rule with the same name.
7a. If such rule does not exist, create a new one.
If ((Get-AzureSqlDatabaseServerFirewallRule -ServerName hq4g4hz60d -RuleName "ClientIPAddress_Dynamic") -eq $null) {
New-AzureSqlDatabaseServerFirewallRule -ServerName hq4g4hz60d
-RuleName "ClientIPAddress_Dynamic" -StartIpAddress $currentIP -EndIpAddress $currentIP
}
7b. If such rule does exist, update it to allow database connections originating from $currentIP.
else {
Set-AzureSqlDatabaseServerFirewallRule -ServerName hq4g4hz60d
-RuleName "ClientIPAddress_Dynamic" -StartIpAddress $currentIP -EndIpAddress $currentIP
}