Powershell: Import database in Microsoft Azure

In The previous article we discussed about Powershell Script to add Firewall Rule in SQL Server in Microsoft Azure

    Import database into exsting SQL Server in Azure Environment


    This script creates a new database from the backpac file available in the Storage Container


    .\ImportDBtoAzure.ps1 -AzureSubscriptionName "dt - Dev" -StorageAccountName dtdevstorageaccount -StorageContainer testdt -ServerName "vzem96um0h" -DatabaseName dtdb9883 -BACPAC dt9883.BACPAC
Powershell Script to Import database in Microsoft Azure

[CmdletBinding(SupportsShouldProcess = $true)]

    # The name of valide Azure Subscription associated with Account
    [Parameter(Mandatory = $true)]

    # The name of the storage account name to copy files to.

    [Parameter(Mandatory = $true)]

    # The name of the storage container to copy files to.

    [Parameter(Mandatory = $true)]

    # The name of the SQL Server name on which database will be imported.

    [Parameter(Mandatory = $true)]

    # The name of new database.

    [Parameter(Mandatory = $true)]

    # The name of BACPAC file available in the Container.

    [Parameter(Mandatory = $true)]


# The script has been tested on Powershell 3.0

Set-StrictMode -Version 3

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

# To authenticate through Windows Azure Active Directory and downloads associated subscriptions.


# To select specific subscription from available subscription "Visual Studio Enterprise with MSDN"

Select-AzureSubscription -SubscriptionName $AzureSubscriptionName

# Check if Windows Azure Powershell is avaiable

if ((Get-Module -ListAvailable Azure) -eq $null)
    throw "Windows Azure Powershell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
    Write-Host "Windows Azure Powershell is available!!!"

#Running the Get-Credential cmdlet opens a window asking for your username and password. Enter the admin login and password for the SQL server you want to create the database in $ServerName

$credential = Get-Credential

$SqlCtx = New-AzureSqlDatabaseServerContext -ServerName $ServerName -Credential $credential

Write-Host $SqlCtx

$StorageKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName

$StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageKey.Primary

$Container = Get-AzureStorageContainer -Name $StorageContainer -Context $StorageContext

# Import Database Operation

$ImportRequest = Start-AzureSqlDatabaseImport -SqlConnectionContext $SqlCtx -StorageContainer $Container -DatabaseName $DatabaseName -BlobName $BACPAC
Get-AzureSqlDatabaseImportExportStatus -RequestId $ImportRequest.RequestGuid -ServerName $ServerName -Username $credential.UserName

In the next Article we will discuss about Automation: Powershell Script to create SQL Server in Microsoft Azure

Labels: , , ,