繁体   English   中英

使用 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 得到这个工作?

根据我的测试,rest 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"

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM