Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PowerShell在Azure中创建新的警报规则?_Azure_Powershell_Azure Powershell_Azure Cli - Fatal编程技术网

如何使用PowerShell在Azure中创建新的警报规则?

如何使用PowerShell在Azure中创建新的警报规则?,azure,powershell,azure-powershell,azure-cli,Azure,Powershell,Azure Powershell,Azure Cli,我快发疯了。我该如何使用PowerShell在Azure中创建新的警报规则 有了Azure CLI,就这么简单了 # Retrieve id for scope parameter az vm show -n vm1 -o tsv --query id # Create action group az monitor action-group create -n action-group1 -g rg-training --action email admin adminaddress@ho

我快发疯了。我该如何使用PowerShell在Azure中创建新的警报规则

有了Azure CLI,就这么简单了

# Retrieve id for scope parameter
az vm show -n vm1 -o tsv --query id 

# Create action group
az monitor action-group create -n action-group1 -g rg-training --action email admin adminaddress@host.org

# Create alert
az monitor metrics alert create -n alert2 -g rg-training --scopes "/subscriptions/<guid>/resourceGroups/rg-training/providers/Microsoft.Compute/virtualMachines/vm1" --condition "avg Percentage CPU > 90" --window-size 5m --evaluation-frequency 1m --action action-group1 --description "High CPU"
导致

> Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException,
> Message: Null/Empty, Code: Null, Status code:BadRequest, Reason
> phrase: Bad Request At C:\Temp\03 Create and test alerts.ps1:17 char:1
> + Add-AzMetricAlertRuleV2 -Name "metricRule2" -ResourceGroupName "rg-tr ...
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo          : CloseError: (:) [Add-AzMetricAlertRuleV2], PSInvalidOperationException
> + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetricAlertRuleV2Command

我在这里做错了什么?

与此同时,我已经弄明白了。。。罪魁祸首是我取回$targetResourceId的方式。以下内容对我很有用

# TypeName: Microsoft.Azure.Commands.Insights.OutputClasses.PSActionGroupResource
$actionGroup = Get-AzActionGroup -Name "my-action-group" -ResourceGroupName "rg-training" 

# TypeName: Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup
$actionGroupId = New-AzActionGroup -ActionGroupId $actionGroup.Id

# TypeName: System.TimeSpan
$windowSize = New-TimeSpan -Minutes 1

# TypeName: System.TimeSpan
$frequency = New-TimeSpan -Minutes 1 

# TypeName: Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricCriteria
$condition = New-AzMetricAlertRuleV2Criteria -MetricName "Percentage CPU" -TimeAggregation Average -Operator GreaterThan -Threshold 0.1

# TypeName: System.String
$targetResourceId = (Get-AzResource -Name VM1).ResourceId

# TypeName: Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext
$context = Get-AzContext

Add-AzMetricAlertRuleV2 -Name "metricRule" -ResourceGroupName "rg-training" -WindowSize $windowSize -Frequency $frequency -TargetResourceId $targetResourceId -Condition $condition -ActionGroup $actionGroupId -Severity 3

同时我也发现了。。。罪魁祸首是我取回$targetResourceId的方式。以下内容对我很有用

# TypeName: Microsoft.Azure.Commands.Insights.OutputClasses.PSActionGroupResource
$actionGroup = Get-AzActionGroup -Name "my-action-group" -ResourceGroupName "rg-training" 

# TypeName: Microsoft.Azure.Management.Monitor.Management.Models.ActivityLogAlertActionGroup
$actionGroupId = New-AzActionGroup -ActionGroupId $actionGroup.Id

# TypeName: System.TimeSpan
$windowSize = New-TimeSpan -Minutes 1

# TypeName: System.TimeSpan
$frequency = New-TimeSpan -Minutes 1 

# TypeName: Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricCriteria
$condition = New-AzMetricAlertRuleV2Criteria -MetricName "Percentage CPU" -TimeAggregation Average -Operator GreaterThan -Threshold 0.1

# TypeName: System.String
$targetResourceId = (Get-AzResource -Name VM1).ResourceId

# TypeName: Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext
$context = Get-AzContext

Add-AzMetricAlertRuleV2 -Name "metricRule" -ResourceGroupName "rg-training" -WindowSize $windowSize -Frequency $frequency -TargetResourceId $targetResourceId -Condition $condition -ActionGroup $actionGroupId -Severity 3

您正在使用哪一版本的azure cli?用于答复的Thx。我不知道为什么这会有关联,但你现在看到的是2.5.1你是对的,我的意思是az powershell版本-Get InstalledModule-根据此线程命名az:此错误已修复版本3.x你使用哪一版本的azure cli?谢谢回复。我不知道为什么这会是相关的,但这里你去2.5.1你是对的,我的意思是az powershell版本-得到安装模块-名称az根据这个线程:这个错误已修复版本3.X