PatchResourceNotFound error while calling azure rest API

你。 提交于 2019-12-02 11:44:44

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:

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:

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}},@{Name="Workspace";Expression={[regex]::Match($.properties.source.dataSourceId,"(?<=/workspaces/)(.)").value}},@{Name="Resource Group";Expression={[regex]::Match($_.properties.source.dataSourceId,"(?<=/resourceGroups/)(.)(?=/providers)").value}} | Format-Table $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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!