How to update smart detection settings alerts through powershell scripts - azure

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:

Related

Azure DataFactory self hosted integration runtime is now shown in portal

I have created a self-hosted integration runtime using powershell :
Set-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $resourceGroupName -DataFactoryName $dataFactoryName -Name $selfHostedIntegrationRuntimeName -Type SelfHosted -Description "selfhosted IR description"
But when I check the Azure Datafactory portal and then select Integration Runtime I can't see it. When I create via. portal it's visible to me.
I can check its status in Powershell it exists there.
Get-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $resourceGroupName -DataFactoryName $dataFactoryName -Name $selfHostedIntegrationRuntimeName -Status
I was even able to register it and I see its status is online but nothing is shown in the portal.
Is there any reason it's not shown?
I found the solution.
In Portal it's showing by default from master branch.
I needed to switch to live mode to see all integrations.

How to list metric alerts in Azure with PowerShell

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)

Azure Logic Apps: how to run Powershell script or Azure CLI?

I'm building my Azure Logic Apps worklow which is supposed to check some conditions and run following Powershell:
Stop-AzureWebsiteJob -Name MyWebsite -JobName MyWebJob
Start-AzureWebsiteJob -Name MyWebsite -JobName MyWebJob -JobType Continuous
The question is: what's the easiest way to invoke such script in Azure Logic Apps? It seems like there's no built in block/connector for Powershell so I'd like to know what are the possibilites. Or perhaps it might be easier to run az CLI command with similar operation
Finally I ended up with a solution which takes advantage of Azure Automation. From Azure Portal we can create new Resource typing in Automation:
Once the resource is created we can add new Runbook under runbooks tab:
Runbook can run Powershell Workflow and get authorized using AzureRunAsConnection option (details here). My sample Powershell which is supposed to restart WebJob an specific App Service looks like below:
Workflow RestartMyWebJob
{
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
$AzureContext = Select-AzureRmSubscription -SubscriptionId $Conn.SubscriptionID
$Apiversion = "2015-08-01"
$ResourceGroupName = 'My-Resource-Group-Name'
$ResourceName = 'My-Resource-Group-Name/My-AppService--WebJob-Name'
Invoke-AzureRmResourceAction -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName $ResourceName -Action stop -ApiVersion $Apiversion -Force
Invoke-AzureRmResourceAction -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName $ResourceName -Action start -ApiVersion $Apiversion -Force
}
Having this Workflow setup we can run it from Azure Logic Apps by adding new block to our logic.
Currently, azure logic seems not support to run powershell and cli script, here is a voice in azure feedback, you could vote it.
Workaround:
If you want to start and stop the webjob, you can call the Kudu WebJobs API in the logic app.
You can follow the steps below.
1.Run the powershell command locally to generate the Authorization token of your web app.
$creds = Invoke-AzureRmResourceAction -ResourceGroupName joywebapp -ResourceType Microsoft.Web/sites/config -ResourceName joywebapp2/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$username = $creds.Properties.PublishingUserName
$password = $creds.Properties.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
The $base64AuthInfo is what we need, it should be like JGpveXdlYmFwcDI6NnJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzRktSdXlUcU5acUUzdFhNb05j.
The token will never be changed except you reset the publish profile, so you just need to do this step once.
2.In the logic app, specific the Method, URI, Headers(The header should be like
Authorization: Basic JGpveXdlYmFwcDI6NnJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzRktSdXlUcU5acUUzdFhNb05j, note use space to separate the Basic and token), for example , I start a triggered webjob in my web app.
Triggered result:
So you just need to follow the steps above, for your issue, refer to the APIS:
Start a continuous job
Stop a continuous job
Create an Azure Function with an http trigger with Powershell as the function language (or any other supported language). Then you call the Function easily in the Logic app by calling an Http endpoint.
actually nowdays Azure provide this option, without creating runbooks and automation accounts. It is still in preview mode, but seems to be working !
You can also have your PowerShell code run in an Azure Container Instance supporting PowerShell and create an new Container Group from the Logic App workflow.

Azure Portal Data Factory Monitor & Manate Error 404 File or Directory not found

I have created an Azure Data Factory pipeline and deployed it. No issues. When I am in the Azure portal Data Factory blade, I click on the Monitor & Manage button and it takes me to a new tab in MS Edge with the following error:
404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
Does anyone know what I need to do to be able to monitor my Azure Data Factory pipeline activity?
Thank you.
Not sure about the 404 error, maybe just an authentication issue. Or permissions in your Azure directory.
To monitor a pipeline or ADF in general I would suggest using PowerShell. There are loads of cmdlets to do things beyond the Azure Portal UI, including set a time slice status.
For example, to check what's currently 'In Progress' in your factory do something like this...
Import-Module Azure
#Params...
$AzureUser = "" # <<< enter username
$AzurePass = "" #<<< enter password
$AzureSubscription = "" # <<< enter subscription name
$ResourceGroup = "" # <<< enter resource group name
#Create credential
$SecurePassword = ConvertTo-SecureString $AzurePass -AsPlainText -Force
$PSCredential = New-Object System.Management.Automation.PSCredential ($AzureUser, $SecurePassword)
#Create Azure Connection
Login-AzureRmAccount -Credential $PSCredential | Out-Null
#Set context for subscription
$SubId = Get-AzureSubscription `
-SubscriptionName $AzureSubscription | SELECT SubscriptionId
Set-AzureRmContext -SubscriptionId $SubId.SubscriptionId | Out-Null
#Get ADF details
$ADFName = Get-AzureRmDataFactory `
-ResourceGroupName $ResourceGroup | SELECT DataFactoryName
Get-AzureRmDataFactoryActivityWindow `
-DataFactoryName $ADFName.DataFactoryName `
-ResourceGroupName $ResourceGroup `
| ? {$_.WindowState -eq "InProgress"}
Here's a link to the other cmdlets.
https://learn.microsoft.com/en-us/powershell/resourcemanager/azurerm.datafactories/v2.5.0/azurerm.datafactories
Hope this helps.
Thank you Paul, I want to start getting more familiar with Azure Powershell cmdlets.
I was able to access the portal Monitor & Manage by opening the portal in Chrome. Seems strange that MS Azure doesn't work in MS Edge but it does work in Chrome.
Thank you for you response.
Jon

Azure: Powershell: Set-AzureRmWebApp: How to set the "alwaysOn" property

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.

Resources