[英]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请告诉我一个解决方案
You need to create an ActionGroup reference object in memory.您需要在内存中创建一个 ActionGroup 引用对象。 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.加入这一行
$actionGrp1 = New-AzureRmActionGroup -ActionGroupId $actionGrp.Id
到你的脚本,并更改-Action $actionGrp
至-Action $actionGrp1
在最后一行,则它会工作。
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
Note : I test with the new Az
module, for the AzureRm
module, it is the same logic.注意:我使用新的
Az
模块进行测试,对于AzureRm
模块,它是相同的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.