azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

[Insights][Monitor] - Can't create ActivityLogAlert with an already created ActionGroup.

Open AngshumanNayakMSFT opened this issue 8 years ago • 12 comments

Cmdlet(s)

Set-AzureRmActivityLogAlert

PowerShell Version

Instructions: to get PowerShell version, type $PSVersionTable and look for the value associated with PSVersion

Module Version

OS Version

Instructions: to get OS version, type $PSversionTable and look for value associated with BuildVersion

Description

Debug Output

Instructions: to get Debug Output, set $DebugPreference="Continue" and then execute the cmdlet or script causing the issue

PS C:\WINDOWS\system32> Set-AzureRmActivityLogAlert -Location $location -Name 'MaintenanceAlert' -ResourceGroupName 'Default-ActivityLogAlerts' ` -Scope '/subscriptions/40cba20b-8e19-4f07-9042-b1b6a85fe7de' -Action $AGOnlyEmail -Condition $condition -Verbose Set-AzureRmActivityLogAlert : Cannot bind parameter 'Action'. Cannot convert the "Microsoft.Azure.Commands.Insights.OutputClasses.PSActionGroupResource" value of type "Microsoft.Azure.Commands.Insights.OutputClasses.PSActionGroupResource" to type "Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup". At line:2 char:98

  • ... ns/40cba20b-8e19-4f07-9042-b1b6a85fe7de' -Action $AGOnlyEmail -Condi ...
  •                                                  ~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Set-AzureRmActivityLogAlert], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.Insights.ActivityLogAlert.SetAzur eRmActivityLogAlertCommand

Script/Steps for Reproduction

$email = New-AzureRmActionGroupReceiver -Name 'user1' -EmailReceiver -EmailAddress '[email protected]' $AGOnlyEmail = Set-AzureRmActionGroup -Name 'agOnlyEmail' -ResourceGroup 'Default-ActivityLogAlerts' -ShortName 'testEmail' -Receiver $email $condition = New-AzureRmActivityLogAlertCondition -Field 'category' -Equal 'ServiceHealth'

echo $AGOnlyEmail

Set-AzureRmActivityLogAlert -Location $location -Name 'MaintenanceAlert' -ResourceGroupName 'Default-ActivityLogAlerts' ` -Scope '/subscriptions/40cba20b-8e19-4f07-9042-b1b6a85fe7de' -Action $AGOnlyEmail -Condition $condition -Verbose

AngshumanNayakMSFT avatar Feb 15 '18 00:02 AngshumanNayakMSFT

@micongjie It seems that the New-AzureRmActionGroup is returning the SDK type instead of the PowerShell type. Additionally, Set-AzureRmActivityLogAlert takes in the SDK type for -Action instead of the PowerShell type. Set-AzureRmActionGroup returns the correct PowerShell type, so the 2 other commands should use that type (PSActionGroupResource). Since this would be a breaking change, could you look into making a warning in the near term, and then implementing these changes for the breaking change release in May?

MiYanni avatar Feb 15 '18 17:02 MiYanni

@AngshumanNayakMSFT So, it seems that New-AzureRmActionGroup will give you a type that can be used by -Action in Set-AzureRmActivityLogAlert. But, if we're going to change this, for now, I'd recommend trying to create a Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup by hand using New-Object. You'd have to use the properties from the PSActionGroupResource to create this object.

MiYanni avatar Feb 15 '18 17:02 MiYanni

@gucalder This is activity log alert cmdlet. Can you take a look?

micongjie avatar Feb 15 '18 17:02 micongjie

Thanks @MiYanni ! I was able to create a new object to type and make it work.

$email = New-AzureRmActionGroupReceiver -Name 'user1' -EmailReceiver -EmailAddress '[email protected]'
$AGOnlyEmail = Set-AzureRmActionGroup -Name 'agOnlyEmail' -ResourceGroup 'Default-ActivityLogAlerts' -ShortName 'testEmail' -Receiver $email
$condition = New-AzureRmActivityLogAlertCondition -Field 'category' -Equal 'ServiceHealth'
$AGAlertObject = New-Object Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup
$AGAlertObject.ActionGroupId = $AGOnlyEmail.Id 

Have pushed a PR with additional example under Set-AzureRmActivityLogAlert here https://github.com/Azure/azure-docs-powershell/pull/501 .

AngshumanNayakMSFT avatar Feb 15 '18 18:02 AngshumanNayakMSFT

So Set-AzureRmActivityLogAlert takes the SDK type for -Action instead of the PowerShell type and only Set-AzureRmActionGroup returns the proper type? What about Get-AzureRMActionGroup? I don't think we should have to create a new Action Group for every alert.

Sergmis avatar May 30 '18 20:05 Sergmis

@Sergmis It looks like Get-AzureRmActionGroup returns a list of the PowerShell type, Microsoft.Azure.Commands.Insights.OutputClasses.PSActionGroupResource. You can do the same workaround that @AngshumanNayakMSFT did above, except use Get-AzureRmActionGroup instead of Set-AzureRmActionGroup.

MiYanni avatar May 30 '18 20:05 MiYanni

Thanks @MiYanni got it to create a new ActivityLog alert using $AGOnlyEmail = Get-AzureRmActionGroup -Name 'MyTeamAlerts' -ResourceGroup 'my-rg' . Figured out appropriate New-AzureRmActivityLogAlertCondition conditions for category equal to Administrative from existing alerts in New Alerts Blade (https://portal.azure.com/#blade/Microsoft_Azure_Monitoring/AlertsBlade)

Sergmis avatar May 30 '18 22:05 Sergmis

Hey Hello. Can i just throw a quick question in here. Why is Action a required parameter in the PowerShell CMDlet but in the Azure CLI version it is optional?

https://docs.microsoft.com/en-us/cli/azure/monitor/activity-log/alert?view=azure-cli-latest#az-monitor-activity-log-alert-create

jonas-wanninger avatar Jul 19 '18 11:07 jonas-wanninger

Thanks a lot Angshuman!! The Cmdlet works fine to create ActivityLogAlert to use an existing Action Group.

shashikrish avatar Sep 04 '18 07:09 shashikrish

Thanks @MiYanni ! I was able to create a new object to type and make it work.

$email = New-AzureRmActionGroupReceiver -Name 'user1' -EmailReceiver -EmailAddress '[email protected]'
$AGOnlyEmail = Set-AzureRmActionGroup -Name 'agOnlyEmail' -ResourceGroup 'Default-ActivityLogAlerts' -ShortName 'testEmail' -Receiver $email
$condition = New-AzureRmActivityLogAlertCondition -Field 'category' -Equal 'ServiceHealth'
$AGAlertObject = New-Object Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup
$AGAlertObject.ActionGroupId = $AGOnlyEmail.Id 

Have pushed a PR with additional example under Set-AzureRmActivityLogAlert here Azure/azure-docs-powershell#501 .

Hi Angshuman,

Thank you for the reference. I tried below query in my environment but getting bad request error. Any suggestion where i am doing mistake.

Connect-AzureRmAccount Select-AzureRmSubscription -SubscriptionName "subscription name" $location = 'Global' $alertName = 'Critical Power Off - Alert' $resourceGroupName = 'Resource Group name' $wHook = New-AzureRmActionGroupReceiver -Name "WHook" -WebhookReceiver -ServiceUri "http://www.google.com" $actionGrp1 = Set-AzureRmActionGroup -Name 'CreateSNTicket' -ResourceGroup $resourceGroupName -ShortName 'SNTicket' -Receiver $wHook $condition1 = New-AzureRmActivityLogAlertCondition -Field 'level' -Equal 'Critical' $condition2 = New-AzureRmActivityLogAlertCondition -Field 'status' -Equal 'Failed' $condition0 = New-AzureRmActivityLogAlertCondition -Field 'Category' -Equal 'Administrative' $AGAlertObject = New-Object Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup $AGAlertObject.ActionGroupId = $actionGrp1.Id Set-AzureRmActivityLogAlert -Location $location -Name $alertName -ResourceGroupName $resourceGroupName -Scope "Computer Name" -Action $AGAlertObject -Condition $condition0,$condition1,$condition2 -Verbose

code-ninja99 avatar Oct 03 '18 11:10 code-ninja99

we added this item to our backlog to review. thanks

nolavime avatar Jan 01 '20 21:01 nolavime

:bell: Routing this issue to @act-observability-squad.