【问题标题】:getting error while creating PowerShell script for alert rules in azure在 azure 中为警报规则创建 PowerShell 脚本时出错
【发布时间】:2019-12-04 17:15:21
【问题描述】:
$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

但是每次我运行这段代码时,我都会收到如下所述的错误:

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

请告诉我解决办法

【问题讨论】:

    标签: azure powershell


    【解决方案1】:

    您需要在内存中创建一个 ActionGroup 引用对象。将此行$actionGrp1 = New-AzureRmActionGroup -ActionGroupId $actionGrp.Id添加到您的脚本中,并将最后一行中的-Action $actionGrp更改为-Action $actionGrp1,然后它就可以工作了。

    完整脚本

    $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
    

    注意:我用新的Az模块测试,AzureRm模块的逻辑是一样的。

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2020-04-16
      • 1970-01-01
      • 2015-12-15
      • 2021-11-25
      • 2021-10-31
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多