[英]Invoke Rest API from an Azure Runbook ( Powershell) to scale up and Down an Azure Synapse Database
我創建了一個 Azure Powershell Runbook 以使用 Invoke-RestMethod 向上或向下擴展數據倉庫專用 SQL 池,但失敗並出現以下錯誤:
At line:31 char:11 + $Body = @ + ~ Unrecognized token in source text. At line:36 char:6 + "name" = "DW500c" + ~~~~~~ The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
這是正在使用的代碼
$Url = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$sqlServerName/databases/$SynapseSqlPoolName?api-version=2020-08-01-preview HTTP/1.1 Content-Type: application/json; charset=UTF-8"
$Body = @
{
location: "West Europe",
"sku":
{
"name" = "DW500c"
}
}
Invoke-RestMethod -Method POST -Uri $url -Body $body -UseBasicParsing
我試圖用“:”更改“=”,但它給出了相同的錯誤
我已經嘗試使用 Mathias 提供的以下解決方案,但我收到了一個新錯誤
$Body = @{
location = "West Europe"
sku = @{
name = "DW500c"
}
}
錯誤:
遠程服務器返回錯誤 (400) Bad Request
Invoke-RestMethod : {"error":{"code":"MissingApiVersionParameter","message":"The api-version query parameter (?api-version=) is required for all requests."}} At line:38 char:3 + Invoke-RestMethod -Method POST -Uri $url -Body $body -UseBasi ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
最新代碼但出現“指定的內容類型無效”
$Url = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$sqlServerName/databases/$SynapseSqlPoolName?api-version=2020-08-01-preview"
$ContentType = "HTTP/1.1 Content-Type:application/json;charset=UTF-8"
$Body = @{
location = "West Europe"
sku = @{
name = "DW500c"
}
}
Invoke-RestMethod -Method POST -Uri $url -ContentType $ContentType -Body $body -UseBasicParsing
內容類型的問題已得到修復,但我仍然收到 api 版本錯誤。 下面是完整的代碼
Param
(
# This is the Resource group where Azure Synapse Analytics SQL Pool is located
[Parameter(Mandatory=$True)]
[String] $resourceGroupName
,
# This is the name of the Azure SQL Server hosting the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $sqlServerName
,
# This is the name of the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $SynapseSqlPoolName
,
# This is the name of the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $SubscriptionId
,
# This is the name of the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $Sku
)
$ConnectionName = 'AzureRunAsConnection'
$ServicePrincipalConnection = Get-AutomationConnection -Name $ConnectionName
'Log in to Azure...'
$null = Connect-AzAccount `
-ServicePrincipal `
-TenantId $ServicePrincipalConnection.TenantId `
-ApplicationId $ServicePrincipalConnection.ApplicationId `
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$Url = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$sqlServerName/databases/$SynapseSqlPoolName?api-version=2020-08-01-preview"
$ContentType = "application/json;charset=UTF-8"
$Body = @{
location = "West Europe"
sku = @{
name = $Sku
}
}
Invoke-RestMethod -Method POST -Uri $url -ContentType $ContentType -Body $body -UseBasicParsing
我試圖用“:”更改“=”,但它給出了相同的錯誤
這是正確的做法,但請確保您也對內部sku
object 使用哈希表文字 ( @{... }
):
$Body = @{
location = "West Europe"
sku = @{
name = "DW500c"
}
}
似乎您分配給$Url
的字符串值包含的不僅僅是 URL - 從 URL 中刪除尾隨的 HTTP header 垃圾,然后將 Content-Type header 值傳遞給Invoke-RestMethod
-ContentType
$Url = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$sqlServerName/databases/$SynapseSqlPoolName?api-version=2020-08-01-preview"
$ContentType = 'application/json;charset=UTF-8'
# ...
Invoke-RestMethod -Method POST -Uri $url -ContentType $ContentType -Body $body -UseBasicParsing
感謝 Mathias R.Jessen 的指導,感謝我審查過的 100 個網站以及今天進行的 100 次或更多次測試,我終於找到了讓它工作的方法。 我們需要記住,正常的 Azure 數據庫與 Synapse 數據庫(我的是 Synapse DB)有不同的 URL。
可以對代碼進行更多調整,例如自動獲取 subscriptionId 和 apiversion 而不是對其進行硬編碼,還可以使用一些 try、catch 和 IF 條件在未暫停但代碼時對 Synapse DB 進行縮放下面完成使用 Azure 中的 Powershell Runbook 擴展和縮小 Snapse DB 所需的操作(調用 API)
Param
(
# This is the Resource group where Azure Synapse Analytics SQL Pool is located
[Parameter(Mandatory=$True)]
[String] $resourceGroupName
,
# This is the name of the Azure SQL Server hosting the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $sqlServerName
,
# This is the name of the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $SynapseSqlPoolName
,
# This is the name of the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $Sku
,
# This is the name of the Azure Synapse Analytics SQL Pool
[Parameter(Mandatory=$True)]
[String] $SubscriptionId
)
$ConnectionName = 'AzureRunAsConnection'
$ServicePrincipalConnection = Get-AutomationConnection -Name $ConnectionName
'Log in to Azure...'
$null = Connect-AzAccount `
-ServicePrincipal `
-TenantId $ServicePrincipalConnection.TenantId `
-ApplicationId $ServicePrincipalConnection.ApplicationId `
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$azContext = Get-AzContext
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
$authHeader = @{
'Content-Type'='application/json'
'Authorization'='Bearer '+$token.AccessToken
}
$apiversion = "2021-06-01"
$Url = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Synapse/workspaces/$sqlServerName/sqlPools/$SynapseSqlPoolName"+"?api-version=$apiversion"
$ContentType = "application/json;charset=UTF-8"
$Body = @{
location = "westeurope"
sku = @{
name = $Sku
}
}
Invoke-RestMethod -Uri $Url -Method PUT -Headers $authHeader -Body ($body|ConvertTo-Json) ## -UseBasicParsing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.