databaseazurepowershellbacpac

Exporting azure database in powershell with New-AzSqlDatabaseExport does not always return the OperationStatusLink, resulting in an exception


I am writing a powershell script to export an Azure database to a bacpac file using the New-AzSqlDatabaseExport command (following the documentation here.

When I run the powershell script, the results I get are inconsistent. When I open a new powershell window and run the export database script, everything runs as expected, and I get back an OperationStatusLink, so I can check the progress of the export as it progresses. However, once the export completes, if I try running the powershell script a 2nd time within the same window, the export will not return the OperationStatusLink. This will cause Get-AzSqlDatabaseImportExportStatus to fail with the following exception: Cannot bind argument to parameter 'OperationStatusLink' because it is null.

Below are the steps to reproduce, as well as a snippet of powershell script. Any suggestions as to what I could possibly try to ensure that New-AzSqlDatabaseExport always returns an OperationStatusLink would be greatly appreciated.

Steps to Reproduce:

  1. Open powershell window

  2. Log in to Azure

  3. Run script to export database to bacpac

    Expected Result: Export is successful and OperationStatusLink is provided

    Actual Result: Export is successful and OperationStatusLink is provided

  4. Run script to export database to bacpac

    Expected Result: Export is successful and OperationStatusLink is provided

    Actual Result: Export is successful and OperationStatusLink is not provided

Powershell script:

Connect-AzAccount
Select-AzSubscription -SubscriptionName 'subscription name'

BackupAzureDatabase.ps1 `
 -DatabaseName "testDB" `
 -ResourceGroupName "group1" `
 -ServerName "testserver" `
 -serverAdmin "admin" `
 -serverPassword "********" `

BackupAzureDatabase.ps1:

Param(
    [string][Parameter(Mandatory=$true)] $DatabaseName,
    [string][Parameter(Mandatory=$true)] $ResourceGroupName,
    [string][Parameter(Mandatory=$true)] $ServerName,
    [string][Parameter(Mandatory=$true)] $ServerAdmin,
    [string][Parameter(Mandatory=$true)] $ServerPassword,
)
Process{
    # some code to get the storage info and credentials

    $ExportRequest = New-AzSqlDatabaseExport `
            -ResourceGroupName $ResourceGroupName `
            -ServerName $ServerName `
            -DatabaseName $DatabaseName `
            -StorageKeytype $StorageKeytype `
            -StorageKey $PrimaryKey `
            -StorageUri $BacpacUri `
            -AdministratorLogin $Creds.UserName `
            -AdministratorLoginPassword $Creds.Password

     $ExportStatus = Get-AzSqlDatabaseImportExportStatus `
                -OperationStatusLink $ExportRequest.OperationStatusLink

     # Get-AzSqlDatabaseImportExportStatus throws an exception, since OperationStatusLink is empty/null most of the time
}

Solution

  • This seems to be a regression in Az.Sql module introduced in 2.10.0 and it still active with the current version (2.11.0)

    Symptoms: when initiating export operation the following exception raised: New-AzSqlDatabaseExport: Missing the required 'networkIsolation' parameters for ImportExport operation.

    The issue: this should be optional parameter, and the parameter name is incorrect, it should be -UseNetworkIsolation instead.

    Workaround: target your script to older version of the module, 2.9.1 seems to be OK.

    Long term solution: The fix already committed, it should be available in the next releases on the module.

    Source of information: https://github.com/Azure/azure-powershell/issues/13097

    Update 2020-11-04 The recent version of the module already contains the fix. (2.11.1) https://www.powershellgallery.com/packages/Az/5.0.0