问题
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"
https://management.azure.com/subscriptions/subid/resourcegroups/RGname/providers/microsoft.insights/scheduledQueryRules/alertname?api-version=2018-04-16
got below error
{
"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 "$_"
}
}
回答1:
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:
回答2:
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
来源:https://stackoverflow.com/questions/55707774/patchresourcenotfound-error-while-calling-azure-rest-api