将 SQL Azure 数据库导出到 blob - Start-AzureSqlDatabaseExport:无法将 AzureStorageContainer 转换为 AzureStorageContainer

Export SQL Azure db to blob - Start-AzureSqlDatabaseExport : Cannot convert AzureStorageContainer to AzureStorageContainer

我在堆栈上使用 this code found,所有连接都是正确的。

Import-Module Azure 
Import-Module Azure.Storage

Get-AzureRmSubscription –SubscriptionName “Production” | Select-AzureRmSubscription

# Username for Azure SQL Database server
$ServerLogin = "username"

# Password for Azure SQL Database server
$serverPassword = ConvertTo-SecureString "abcd" -AsPlainText -Force


# Establish credentials for Azure SQL Database Server 
$ServerCredential = new-object System.Management.Automation.PSCredential($ServerLogin, $serverPassword) 

# Create connection context for Azure SQL Database server
$SqlContext = New-AzureSqlDatabaseServerContext -FullyQualifiedServerName “myspecialsqlserver.database.windows.net” -Credential $ServerCredential

$StorageContext = New-AzureStorageContext -StorageAccountName 'prodwad' -StorageAccountKey 'xxxxx'
$Container = Get-AzureStorageContainer -Name 'automateddbbackups' -Context $StorageContext

$exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $SqlContext -StorageContainer $Container -DatabaseName 'Users' -BlobName 'autobackupotest.bacpac' -Verbose -Debug

我收到这个错误。我在这上面花了好几个小时。

Start-AzureSqlDatabaseExport : Cannot bind parameter 'StorageContainer'. Cannot convert the 
"Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageContainer" value of type 
"Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageContainer" to type 
"Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageContainer".
At line:31 char:99
+ ... SqlConnectionContext $SqlContext -StorageContainer $Container -Databa ...
+                                                        ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-AzureSqlDatabaseExport], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.WindowsAzure.Commands.SqlDatabase.Database.Cmdlet.StartAzureSqlDatabaseExport

我正在使用 AzureRM 3.8.0

根据你的描述和代码,如果你想启动一个新的sqldatabase export.I建议你可以尝试下面的代码。它会很好用。

$subscriptionId = "YOUR AZURE SUBSCRIPTION ID"

Login-AzureRmAccount
Set-AzureRmContext -SubscriptionId $subscriptionId

# Database to export
$DatabaseName = "DATABASE-NAME"
$ResourceGroupName = "RESOURCE-GROUP-NAME"
$ServerName = "SERVER-NAME"
$serverAdmin = "ADMIN-NAME"
$serverPassword = "ADMIN-PASSWORD" 
$securePassword = ConvertTo-SecureString -String $serverPassword -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serverAdmin, $securePassword

# Generate a unique filename for the BACPAC
$bacpacFilename = $DatabaseName + (Get-Date).ToString("yyyyMMddHHmm") + ".bacpac"

# Storage account info for the BACPAC
$BaseStorageUri = "https://STORAGE-NAME.blob.core.windows.net/BLOB-CONTAINER-NAME/"
$BacpacUri = $BaseStorageUri + $bacpacFilename
$StorageKeytype = "StorageAccessKey"
$StorageKey = "YOUR STORAGE KEY"

$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName `
  -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
  -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password

$exportRequest

# Check status of the export
Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink

然后可以使用Get-AzureRmSqlDatabaseImportExportStatus查看详细信息,如下:

我在更新了一些我记不太清楚的 powershell 包后遇到了同样的问题。更新后我的脚本开始失败。

我的解决方案是:

  1. 安装最新的AzureRM from nuget via powershell
  2. 使用 Start-AzureSqlDatabaseExport 的另一个重载,它利用参数

    -StorageContainerName 和 -StorageContext 而不是 -StorageContainer

看起来如果您将参数传递给函数,它将在内部创建容器对象而不会崩溃。