Search code examples
powershellazureazure-automation

Azure Automation - Runbook to auto-restart a web app once a day


I do not have much experience with PowerShell at all - what I'd like to do is use my Azure Automation service to set up a runbook that will automatically restart one of my Azure web apps every night at 1am.

Is it possible to accomplish this with Powershell/Azure Automation?


Solution

  • This is perfectly possible, you need to create an Azure Automation account, create a runbook tied to a schedule and use something like this:

    $connectionName = "AzureRunAsConnection" # this is the default connection created when you provision the Automation account,
                                             # you might need to change this to your own connection name
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         
    
    $null = Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
    
    $null = Select-AzureRmSubscription -SubscriptionId 'SUB_GUID' ` # Needed if you have more than 1 subscription
    
    Restart-AzureRmWebApp -ResourceGroupName xxx -Name WebAppName