shristi
shristi

Reputation: 119

getting error while creating PowerShell script for alert rules in azure

$condition1 = New-AzureRmActivityLogAlertCondition -Field 'category' -Equal 'Administrative'
$condition2 = New-AzurermActivityLogAlertCondition -Field  'resourceType' -Equal 'Microsoft.Network/NetworkSecurityGroups'
$email1 = New-AzureRmActionGroupReceiver -Name 'alertget' -EmailReceiver -EmailAddress '<emailaddress>'
$actionGrp=Set-AzureRmActionGroup -Name "withpowershell" -ResourceGroup "<rgname>" -ShortName "Palert" -Receiver $email1
Set-AzurermActivityLogAlert -Location 'Global' -Name 'alertme' -ResourceGroupName '<rgname>' -Scope '/subscriptions/<subsID>' -Action $actionGrp -Condition $condition1,$condition2

But everytime i run this code i get an error as mentioned below:

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:1 char:163
+ ... ions/911df94d-12e9-4695-a90f-943a1bef518d' -Action $actionGrp -Condit ...
+                                                        ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-AzureRmActivityLogAlert], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.Insights.ActivityLogAlert.SetAzureRmActivityLogAlertCommand

Please let me know a solution

Upvotes: 2

Views: 481

Answers (1)

Joy Wang
Joy Wang

Reputation: 42063

You need to create an ActionGroup reference object in memory. Add this line $actionGrp1 = New-AzureRmActionGroup -ActionGroupId $actionGrp.Id to your script, and change the -Action $actionGrp to -Action $actionGrp1 in the last line, then it will work.

Complete script:

$condition1 = New-AzureRmActivityLogAlertCondition -Field 'category' -Equal 'Administrative'
$condition2 = New-AzurermActivityLogAlertCondition -Field  'resourceType' -Equal 'Microsoft.Network/NetworkSecurityGroups'
$email1 = New-AzureRmActionGroupReceiver -Name 'alertget' -EmailReceiver -EmailAddress '<emailaddress>'
$actionGrp=Set-AzureRmActionGroup -Name "withpowershell" -ResourceGroup "<rgname>" -ShortName "Palert" -Receiver $email1
$actionGrp1 = New-AzureRmActionGroup -ActionGroupId $actionGrp.Id
Set-AzurermActivityLogAlert -Location 'Global' -Name 'alertme' -ResourceGroupName '<rgname>' -Scope '/subscriptions/<subsID>' -Action $actionGrp1 -Condition $condition1,$condition2

enter image description here

Note: I test with the new Az module, for the AzureRm module, it is the same logic.

Upvotes: 1

Related Questions