'How to use Azure Runbooks to restart an Azure Web App on a schedule

We have an Azure Web App that we want to setup an automatic restart on a schedule. If I wanted to use Runbooks for this, how would I add one or multiple apps to automatically restart on different schedules?



Solution 1:[1]

Because Az PowerShell modules now have all the capabilities of AzureRM PowerShell modules and more, we'll retire AzureRM PowerShell modules on 29 February 2024

Here is an updated example with Az PowerShell modules.

filter timestamp {"[$(Get-Date -Format G)]: $_"}       
Write-Output "Script started." | timestamp 

$resourceGroupName = "your group"
$appServiceName = "your app service"
$subscriptionName = "your subscription"
try
{
    $conn = Get-AutomationConnection -Name "AzureRunAsConnection" 
    Connect-AzAccount -ServicePrincipal -Tenant $conn.TenantID -ApplicationId $conn.ApplicationID -CertificateThumbprint $conn.CertificateThumbprint | out-null

    # Select-AzureRmSubscription -SubscriptionName subscriptionName

    $result = Restart-AzWebApp -ResourceGroupName $resourceGroupName -Name $appServiceName

    if($result)
    {
        $state = $result.State
        Write-Output ("Web app restarted and is now $state")  #State should be Running at this point
    }
   else
   {
        Write-Output ("Web app did NOT restart")
   }
}
catch
{
    Write-Output ("Web app did NOT restart")
    throw $_.Exception
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Starina