使用 powershell 将 Azure sql 数据库导出到 blob 存储时出错

Erorr exporting Azure sql database to blob storage with powershell

我正在尝试使用 Azure 管理 API 将 SQL 数据库导出到 blob 存储中的 bacpac 文件。这个过程看起来相当简单:获取令牌,然后:

$sqlAzureBackupHeaders = @{
    Authorization = "Bearer $accessToken"
}

$sqlAzureBackupParams = @{
    storageKey =  "<key-goes-here>",
    storageUri =  "http://mystorageacct.blob.core.windows.net/my-blob-container/export-name.bacpac",
    storageKeyType =  "StorageAccessKey",
    administratorLogin =  "<sql-user>",
    administratorLoginPassword =  "<sql-password>",
    authenticationType =  "SQL"
}

$sqlAzureApiUri = "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Sql/servers/<server-name>/databases/<database-name>/export?api-version=2014-04-01"
Invoke-RestMethod -Uri $sqlAzureApiUri -Method Post -Headers $sqlAzureBackupHeaders -Body $sqlAzureBackupParams

这会导致错误:

Invoke-RestMethod : Receivera:InternalServiceFaultThe server was unable to process the request due to an internal error.  For more 
information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the 
&lt;serviceDebug&gt; configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing 
as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.
At D:\Users\protec-admin\Desktop\run-backups.ps1:140 char:1
+ Invoke-RestMethod -uri $sqlAzureApiUri -Method Post -Headers $sqlAzur ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我尝试使用 Invoke-WebRequest 并将正文转换为 json 字符串 - 结果相同。

当我尝试使用 Postman 进行相同的调用时,它工作正常,因此从 powershell 进行调用时有些问题无法正常工作。

我怎样才能使它在 powershell 中工作?

根据我的测试,其余API只接受json体。请使用 ConvertTo-Json 将正文转换为 json。

例如

$headers=@{"Authorization" = "Bearer "+$token}
$body=@{
  "storageKeyType"= "StorageAccessKey";
  "storageKey"= "<your storage account access key>";
  "storageUri"= "https://blobstorage0516.blob.core.windows.net/sample/testbacpac2.bacpac";
  "administratorLogin"= "<SQL admin>";
  "administratorLoginPassword"= "<SQL admin passsword>";
  "authenticationType"= "SQL"
}|ConvertTo-Json
$sqlAzureApiUri = "https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Sql/servers/<server-name>/databases/<database-name>/export?api-version=2014-04-01"
Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $body -UseBasicParsing -ContentType "application/json"