简体   繁体   English

在 azure 中为警报规则创建 PowerShell 脚本时出错

[英]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.

相关问题 Grafana Alert/AzureMonitor:在 Grafana 中为图形创建警报规则时出现执行错误 - Grafana Alert/AzureMonitor: Getting execution error while creating alert rules for graph in Grafana 通过 Powershell 创建 Azure 监控日志搜索警报规则 - Creating Azure Monitor Log Search Alert Rules via Powershell 创建 Azure Application Insights 警报时出错 - Error while creating an Azure Application Insights alert “创建管道时出错。” 在 Azure 管道自托管代理中运行 powershell 脚本时 - "An error occurred while creating the pipeline." when running powershell script in Azure pipeline self-hosted agent 使用命令 New-AzStorageAccount 创建新存储帐户时 azure powershell 出现错误 - Getting error in azure powershell while creating new storage account using the command New-AzStorageAccount 通过Azureure Powershell禁用/启用Azure监视器警报规则 - Disable/Enable Azure monitor alert rules by azure automation powershell Azure Powershell无法创建具有多个端口的NSG规则 - Azure Powershell not creating NSG rules with multiple ports 在OMS中的Log Analytics中创建警报规则时出错 - Getting error while creating Alert rule in Log Analytics in OMS Powershell 获取 Azure 监控警报规则的命令不起作用 - Powershell command to fetch Azure monitor alert rules is not working Azure Powershell在远程系统中创建文件时权限被拒绝错误 - Permission denied error while creating file in remote system by Azure powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM