question

JohnCouch-7779 avatar image
0 Votes"
JohnCouch-7779 asked JohnCouch-7779 commented

Get-AzMetricAlertRuleV2 parse scope in powershell

Trying to pull out the database name from the scopes value. I have tried to access it, but can't seem to figure it out.

https://docs.microsoft.com/en-us/powershell/module/az.monitor/get-azmetricalertrulev2?view=azps-7.1.0


PS C:\>Get-AzMetricAlertRuleV2
TargetResourceId : /subscriptions/00000000-0000-0000-0000-0000000/resourceGroups/metricResourceGroup/providers/Microsoft.KeyVault/vaults/GenevaRPKeyVault
Criteria : {Metric1}
Actions : {/subscriptions/00000000-0000-0000-0000-0000000/resourcegroups/sampleresourcegroup/providers/Microsoft.Insights/actiongroups/scnewactiongroup}
ResourceGroup : metricResourceGroup
Description : fdafda
Severity : 3
Enabled : True
Scopes : {/subscriptions/00000000-0000-0000-0000-0000000/resourceGroups/metricResourceGroup/providers/Microsoft.KeyVault/vaults/GenevaRPKeyVault}
EvaluationFrequency : 00:01:00
WindowSize : 00:05:00
TargetResourceType :
TargetResourceRegion :
AutoMitigate :
LastUpdatedTime :
Id : /subscriptions/00000000-0000-0000-0000-0000000/resourceGroups/metricResourceGroup/providers/Microsoft.Insights/metricAlerts/Rule1
Name : Rule1
Type : Microsoft.Insights/metricAlerts
Location : global
Tags : {}

Criteria : {Metric1}
Actions : {/subscriptions/00000000-0000-0000-0000-0000000/resourcegroups/sampleresourcegroup/providers/Microsoft.Insights/actiongroups/scnewactiongroup}
ResourceGroup : SampleResourceGroup
Description : Testing 1 minute granuarity
Severity : 3
Enabled : True
Scopes : {/subscriptions/00000000-0000-0000-0000-0000000/resourceGroups/SampleResourceGroup/providers/Microsoft.Compute/virtualMachines/SCCMDemo4}
EvaluationFrequency : 00:01:00
WindowSize : 00:01:00
TargetResourceType : Microsoft.Compute/virtualMachines
TargetResourceRegion : eastus
AutoMitigate : True
LastUpdatedTime :
Id : /subscriptions/00000000-0000-0000-0000-0000000/resourceGroups/SampleResourceGroup/providers/Microsoft.Insights/metricAlerts/Rule2
Name : Rule2
Type : Microsoft.Insights/metricAlerts
Location : global
Tags : {}


windows-server-powershellazure-sql-database
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JohnCouch-7779, welcome to Microsoft Q&A forum.

Just wanted to check if you are looking to see the database names in the Scopes value for which metric alert rules are defined in your subscription?

0 Votes 0 ·

Yes. Typically I could parse a string, but I am having trouble getting the scopes out to access that.

0 Votes 0 ·
AnuragSharma-MSFT avatar image
0 Votes"
AnuragSharma-MSFT answered AnuragSharma-MSFT edited

Hi @JohnCouch-7779, thanks for your response again, really appreciate. I am putting your answer as a comment under this answer. Please let us know if you can accept this.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I appreciate the replies, but that did not help. I was able to finally figure out what I was trying to do this way:

 $ResourceGroup = 'ResourceGroup'
    
 $Alerts = (Get-AzMetricAlertRuleV2 -ResourceGroupName $ResourceGroup).Scopes
    
 foreach ($Alert in $Alerts)
 {
 $Alert.SubString(149, $Alert.Length - 149)
 }
1 Vote 1 ·

Hi @JohnCouch-7779, really glad to know you were able to make it work.

If possible, you can mark your answer as Accepted as that could help others facing similar issues.

0 Votes 0 ·

I can't mark my own comment as the answer. there is no option for it.

0 Votes 0 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hi there,

This should be due to a missing default parameter set when this cmdlet has multiple parameter sets.

Here is a thread as well which discusses the same issue and you can try out some troubleshooting steps from this and see if that helps you to sort the Issue.

https://github.com/Azure/azure-powershell/issues/16356
https://github.com/Azure/azure-powershell/issues/16262

Hope this resolves your Query!!



--If the reply is helpful, please Upvote and Accept it as an answer--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.