调用azure rest API时出现PatchResourceNotFound错误

问题描述 投票:0回答:2

当我调用以下日志警报时,azure rest api能够启用/禁用应用程序洞察)同样的api在调用日志分析时创建的警报时抛出错误“PatchResourceNotFound”

https://management.azure.com/subscriptions/subid/resourcegroups/RGname/providers/microsoft.insights/scheduledQueryRules/alertname?api-version=2018-04-16

得到以下错误

{
  "error": {
    "code": "PatchResourceNotFound",
    "message": "The resource 'https://management.azure.com/subscriptions/4776c051-f4ef-4a30-8ce7-c9fb99ff0fc5/resourcegroups/DevOpsTestRG-A/providers/microsoft.insights/scheduledQueryRules/Unexpected shutdown?api-version=2018-04-16' was not found when performing the PATCH operation."
  }
}
Disable-LogAnalyticsAlertRule {
    param(
        [Parameter(Position = 0, mandatory = $true)]
        [string] $Rulename,
        [Parameter(Position = 1, mandatory = $true)]
        [string] $ResourceGroupName
    )

    $headers = Get-AccessTokenFromContext
    $cur_sub = (Get-AzureRmContext).Subscription.Id
    $ruleUri = "https://management.azure.com/subscriptions/$cur_sub/resourcegroups/$resourceGroupName/providers/microsoft.insights/scheduledQueryRules/$RuleName" + "?api-version=2018-04-16"
    $bodyEnable = "
{
    'properties': {
    'enabled': 'false'
    }
}
"
    Write-Verbose "ResourceURI being invoked: $ruleUri"
    try {
        $disablerule = Invoke-RestMethod -Method PATCH -Uri $ruleUri -Headers $headers -Body $bodyEnable
        $disablerule | Select-Object @{Name = "displayName"; Expression = { $_.properties.displayName } }, @{Name = "IsEnabled"; Expression = { $_.properties.enabled } }, @{Name = "lastUpdate"; Expression = { $_.properties.lastUpdatedTime } }, @{Name = "provisioningState"; Expression = { $_.properties.provisioningState } } | Format-Table -AutoSize -Wrap
        Write-Verbose "Output of Invoke-RestMethod: $disablerule"
    }
    catch {
        Write-Error "$_"
    }
}
azure azure-powershell azure-log-analytics
2个回答
1
投票

根据错误消息,我认为评论是正确的:您应该使用%20转义警报名称中的空格,网址如下所示:https://management.azure.com/subscriptions/your_sub/resourcegroups/your_groupResource/providers/microsoft.insights/scheduledQueryRules/Unexpected%20shutdown?api-version=2018-04-16

这是一种快速方法,可以提供正确的URL:

导航到rest api page,单击try it按钮,然后填写所有必要信息,然后它将自动生成一个正确的URL,您可以将其复制以供您在powershell中使用:

enter image description here


0
投票
function Get-AccessTokenFromContext
    {
    try {
        $accesstoken = (New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient([Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile)).AcquireAccessToken((Get-AzureRmContext).Subscription.TenantId).AccessToken
        $buildheaders = @{
            'Authorization' = "Bearer $accesstoken"
            'Content-Type' = "application/json"
                    }
        return $buildheaders
        }
    catch
        {
            Write-Output "No context found! Please run 'Login-AzureRMAccount' to login to Azure"
            break
        }
    }   

function Get-LogAnalyticsAlertRule {param($ cur_sub ='YoursubID',$ resourceGroupName ='RG name')$ headers = Get-AccessTokenFromContext $ cur_sub =(Get-AzureRmContext).Subscription.Id $ ruleidURI =“https://management.azure.com/subscriptions/ $ cur_sub / providers / microsoft.insights / scheduledQueryRules“+”?api-version = 2018-04-16“$ sqrs =(Invoke-RestMethod -Method GET $ ruleidURI -Headers $ headers).value#$ sqrs | Select-Object @ {Name =“DisplayName”; Expression = {$。properties.displayname}},@ {Name =“IsEnabled”; Expression = {$。properties.enabled}},@ {Name =“LastModified”; Expression = {。$ properties.lastUpdatedTime}},@ {名称= “工作区”;表达式= {[正则表达式] ::匹配($ properties.source.dataSourceId, “?(<= /工作区/)()”) .value}},@ {Name =“Resource Group”; Expression = {[regex] :: Match($ _。properties.source.dataSourceId,“(?<= / resourceGroups /)(.)(?=/ providers )“)。value}} |格式表$ sqrs | Select-Object name,@ {Name =“DisplayName”; Expression = {$。properties.displayname}},@ {Name =“IsEnabled”; Expression = {$。properties.enabled}},@ {Name =“Workspace” ; Expression = {[regex] :: Match($。properties.source.dataSourceId,“(?<= / workspaces /)(.*)”)。value}},@ {Name =“Resource Group”; Expression = {[regex] :: Match($。properties.source.dataSourceId,“(?<= / resourceGroups /)(.*)(?=/ providers)”)。value}} | Format-Table -AutoSize -Wrap

    $sqrs_prop = $sqrs.properties

    $rule_name_list = $sqrs_prop.DisplayName
    foreach($rule_name in $rule_name_list){
        Write-Host "disabling $rule_name"
        if($rule_name -ne $null){

        $ruleUri = "https://management.azure.com/subscriptions/$cur_sub/resourcegroups/$resourceGroupName/providers/microsoft.insights/scheduledQueryRules/$rule_name"+"?api-version=2018-04-16"
         $bodyEnable = "
         {
             'properties': {
               'enabled': 'false'
             }
           }
         "
         Write-Verbose "ResourceURI being invoked: $ruleUri"

        try {
            $disablerule = Invoke-RestMethod -Method PATCH -Uri $ruleUri -Headers $headers -Body $bodyEnable
            $disablerule | Select-Object @{Name="displayName";Expression={$_.properties.displayName}}, @{Name="IsEnabled";Expression={$_.properties.enabled}},@{Name="lastUpdate";Expression={$_.properties.lastUpdatedTime}}, @{Name="provisioningState";Expression={$_.properties.provisioningState}} | Format-Table -AutoSize -Wrap
            Write-Verbose "Output of Invoke-RestMethod: $disablerule"
             }
          catch
             {
                Write-Error "$_"
             }
        }
    }
}

GET-LogAnalyticsAlertRule

© www.soinside.com 2019 - 2024. All rights reserved.