【问题标题】:Erorr exporting Azure sql database to blob storage with powershell使用 powershell 将 Azure sql 数据库导出到 blob 存储时出错
【发布时间】:2020-05-18 14:47:14
【问题描述】:

我正在尝试使用 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 得到这个工作?

【问题讨论】:

  • 你是在什么环境下得到这个的?我认为 Invoke-WebRequest 默认需要 IE。您可以使用 -UseBasicParsing 参数解决此问题。
  • 请加-debug获取详细错误信息。

标签: azure powershell rest azure-sql-server azure-management-api


【解决方案1】:

根据我的测试,其余 API 只接受 json 正文。请使用ConvertTo-Json将body转成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"

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 2019-06-10
    • 2021-10-08
    • 1970-01-01
    • 2018-08-08
    • 2018-09-20
    • 2020-03-24
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多