从 Azure Runbook (Powershell) 调用 Rest API 以扩大和缩小 Azure Synapse 数据库

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) 错误请求

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

I've tried to change the "=" with ":" but it gives the same error

这是正确的做法,但请确保您对内部 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