调用Azure Rest API时出现PatchResourceNotFound错误 [英] PatchResourceNotFound error while calling azure rest API

查看:152
本文介绍了调用Azure Rest API时出现PatchResourceNotFound错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在日志警报下方调用时,仅能启用/禁用应用程序见解的azure rest api)调用在日志分析上创建的警报并抛出错误"PatchResourceNotFound"的同一个

when i am calling below log alerts azure rest api able to enable/disable only application insights) same api when calling alerts created on log analytics its throwing error "PatchResourceNotFound"

遇到错误

{
  "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 "$_"
    }
}

推荐答案

根据错误消息,我认为注释是正确的:您应在警报名称中使用%20转义空白,网址如下所示:

As per the error message, I think the comment is right: you should escape the white space in your alert name with %20, the url looks like this: https://management.azure.com/subscriptions/your_sub/resourcegroups/your_groupResource/providers/microsoft.insights/scheduledQueryRules/Unexpected%20shutdown?api-version=2018-04-16

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

Here is an quick way, which can provide correct url:

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

Nav to the rest api page, click on the try it button, and then fill in all the neccessary information, then it will auto generate a correct url which you can copy it for your usage in powershell:

这篇关于调用Azure Rest API时出现PatchResourceNotFound错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆