Is it possible to list metric alerts in Azure with PowerShell?
I could only find:
Get-AzureRmAlertRule
But that only gives me the "classic" alerts and not the metric alerts I have in the portal.
I can get all the metric alerts through the API by using the metricalerts/listbyresourcegroup end-point.
Anyone that has any idea on if it is possible to get them through a PowerShell script?
You could try using the generic Get-AzureRmResource cmdlet with a resource type filter, e.g.
# Retrieve alert rule
$rule = Get-AzureRmResource -ResourceType Microsoft.Insights/alertRules -ResourceGroupName "myResourceGroup" -Name "my-rule";
# Retrieve alerts for this rule
Get-AzureRmAlertHistory -ResourceId $rule.ResourceId -StartTime (Get-Date).AddHours(-1) -EndTime (Get-Date)
Related
I need to fetch the Cost/Month for the VMSize using AZ Powershell.
In the Azure Portal, We can get all the details of Cost/Month,IOPS,vCPU,RAM,etc... when we select the VMSize.
Using cmdlet "Get-AZVMSize -Location "" we get only VMSizeName, no.ofCores,MemoryinMB,MaxDataDiskCount,OSDiskSizeinMB,ResourceDiskSizeinMB only.
The Cost is not coming out. Any AZ Powershell cmdlet which can provide me the Cost/Month for the VMSize based on location?
There is no such cmdlet Out of Box for cost as such. Here is the PowerShell script that uses Get-AzConsumptionUsageDetail to get cost of a VM:
$costTotal=0
$RGName ="RGNAME"
$StartDay="2019-12-01"
$EndDay="2019-12-06"
$VMName="vm1"
$Consumption = Get-AzConsumptionUsageDetail -ResourceGroup $RGName -StartDate
$StartDay -EndDate $EndDay -InstanceName $VMName
$Costs = $Consumption.PretaxCost
foreach ($Cost in $Costs) { $CostTotal += $Cost}
$CostTotal
I have created an alert rule and associated it with a VM. Now trying to fetch the alert rule through Powershell, but getting null. What's wrong with this code ?
Get-AzAlertRule -ResourceGroupName 'pacbldnew'
see the alert rule
powershell code returning null
That is just a warning. The command should work, make sure the alert rule is existing.
Update1:
Try the command below to get what you want.
Get-AzResource -ResourceGroupName joywebapp -ResourceType microsoft.insights/metricAlerts
Update2:
If you want to get the details, try the script as below.
$names = (Get-AzResource -ResourceGroupName joywebapp -ResourceType microsoft.insights/metricAlerts).Name
foreach($name in $names){
Get-AzResource -ResourceGroupName joywebapp -Name $name -ResourceType microsoft.insights/metricAlerts | ConvertTo-Json
}
Joy is right in the way that the cmdlet should still execute as what you see is just a warning. However, this could be happening as Powershell support for newer metric alerts is still in the works as mentioned in the Official docs.
Also, as an alternative, if it helps, you could use Azure CLI to list newer Metric Alerts, as it now supports fetching elaborate results of queries belonging to the Microsoft.Insights/metricAlerts resource type.
For example:
az monitor metrics alert list -g <Resource group name> --output yaml
The result would look something like this:
You also get to choose out of the many output formats (json, jsonc, yaml, table, tsv) available with Az CLI.
Hope this helps!
Go to Azure- home security center and settings and filter and extract all rules
This query has worked for me:
Get-AzResource -ResourceType "microsoft.insights/scheduledqueryrules" -ResourceGroupName "Alert-RG"
Want to update Smart detection setting alerts provided under Azure application insights using powershell cmdlets.
I want to update Smart detection setting alerts provided under Azure application insights using powershell cmdlets, following is a scenario which i want to accomplish.
Scenario: I want to update Failure Anomalies alert and register my emailid under additional email recipients and want to disable the default mail to subscription owner configuration.
Is there any way above mentioned scenario can be accomplished using powershell cmdlets?
Update:
Here is a solution and assume your have azure powershell az module installed(it's ok if you're using powershell azureRM module, but you need to just change the cmdlet respectively):
#the -Name parameter is the Failure Anomalies alert name you see in azure portal, like "Failure Anomalies - your_app_insights_name"
$alets_all = Get-AzAlertRule -ResourceGroupName "xxx" -Name "xxx"
$a = $alets_all[0]
$AppIns = "xxx" #the application insights name
$ResourceGroup = "xxxx"
$SubscriptionId ="xxxx"
$Location =$a.Location
$MetricName =$a.Condition.DataSource.MetricName
$action=New-AzAlertRuleEmail -CustomEmail "abc#gmail.com; xyz#microsoft.com"
$action.SendToServiceOwners=$false
Add-AzMetricAlertRule -Name "Failure Anomalies - $AppIns" -ResourceGroupName $ResourceGroup -TargetResourceId "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroup/providers/microsoft.insights/components/$AppIns" -Operator GreaterThan -Threshold 0 -WindowSize 01:00:00 -Location $Location -TimeAggregationOperator Total -Action $action -MetricName $MetricName
it works well at my side, and test result as below:
In AWS we have instance IDs to identify machines. What's the equivalent in Azure and how do I find that?
I'd like to list the instance ID in powershell so I can put that into a script.
If I have not misunderstood, I suppose you want to get the InstanceId of the VM in the Azure VM Scale set.
You could try to use Get-AzureRmVmssVM to get it.
Get-AzureRmVmssVM -ResourceGroupName <ResourceGroupName> -VMScaleSetName <VMScaleSetName>
Update:
If you want to get the ResourceId of azure resource, you could use Get-AzureRmResource to do it, just specify the properties what you want.
For example:
1.Get ResourceId of the specific resource:
$resource = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType <ResourceType> -ResourceName <ResourceName>
$resource.ResourceId
2.Get ResourceIds of all resources in the specific resource group
$resource = Get-AzureRmResource -ResourceGroupName <ResourceGroupName>
$resource.ResourceId
3.Get ResourceIds of all resources under your subscription.
$resource = Get-AzureRmResource
$resource.ResourceId
The result will like(actual result will decided by your properties):
I am running Powershell 5 and trying to manipulate my Azure WebApp object using Set-AzureRmWebApp (and NOT Set-AzureResource) to set the "Always On" property of the web app.
My basic code snippet starts with a running web app named "myWebApp", and looks like this:
$name = "myWebApp"
$resourceGroupName = "myResourceGroup"
$app_settings = #{"WEBSITE_LOAD_CERTIFICATES"="*";"CommonDatabase"="Common";"WEBSITE_NODE_DEFAULT_VERSION"="0.10.32"}
$result1 = Set-AzureRmWebApp -ResourceGroupName $resourceGroupName -AppSettings $app_settings -Name $name
$result2 = Set-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $this.name -PropertyObject $propertiesObject -ApiVersion 2015-08-01 -Force
The first Set-AzureRmWebApp statement works. It sets all the variables in $app_settings, and they become visible in the Azure Portal blade for myWebApp.
I tried using "Always On"= on as a property in $app_settings with Set-AzureRmWebApp, and it appeared in the App Settings sub-list in the properties of "myWebApp" on the Azure portal blade, but the actual property "Always On" in the general settings remained off.
I read on another site that using Set-AzureRmResource would work, so I tried it, but it failed.
What do I need to do in Powershell to set a property in the General Settings of my Azure WebApp, specifically "Always On"?
"Always On" is not supported if WebApp is in a free Service Plan tier. If the WebApp is in a free tier, please scale up the App Service Plan. Please refer to the document for more info about how to scale up the App Service Plan.Please have a try to use the solution that sharbag mentioned. It is worked for me and I also check the run result from the azure portal. Snipped code from the solution is as followings:
$ResourceGroupName = 'Your Group Name'
$WebAppName = 'Your WebApp Name'
$WebAppPropertiesObject = #{"siteConfig" = #{"AlwaysOn" = $true}}
$WebAppResourceType = 'microsoft.web/sites'
$webAppResource = Get-AzureRmResource -ResourceType $WebAppResourceType -ResourceGroupName $ResourceGroupName -ResourceName $WebAppName
$webAppResource | Set-AzureRmResource -PropertyObject $WebAppPropertiesObject -Force
If the WebApp in a free tier service plan, when we try to run the PowerShell command to enable "Always On" then we will get the following error message.