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

时间:2017-04-26 07:23:16

标签: azure blob azure-blob-storage

我在堆栈上使用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

2 个答案:

答案 0 :(得分:3)

根据您的描述和代码,如果您想开始新的sqldatabase导出。我建议您尝试下面的代码。它会运作良好。

$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查看详细信息,如下所示: enter image description here

答案 1 :(得分:1)

在更新了一些我不记得的PowerShell包后,我遇到了同样的问题。更新后,我的脚本开始失败。

我的解决方案是:

  1. 安装最新的AzureRM from nuget via powershell
  2. 使用利用参数

    Start-AzureSqlDatabaseExport的其他重载

    - StorageContainerName 和 - StorageContext 而不是 - StorageContainer

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