Restart Azure Web App via Web Job with Powershell script

Austin Anderson 0 Reputation points

I am attempting to make a task that restarts our Web Application. I have read that I should be able to use a Web Job in combination with a Powershell script.

What I have tried is a simple powershell script, such as "Restart-AzWebApp -ResourceGroupName "MyResourceGroupName" -Name "MyWebAppName" (per this documentation: and then I have selected that .ps1 file within a new Web Job (along with a CRON time).

When I attempt to manually run/test this Web Job though it says it succeeds but within the output for that web job it says "The term 'Restart-AzWebApp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.", it also does not restart the web application since it errors out.

I am now finding conflicting information around Powershell and its ability to run AZ commands within a Web Job.

This area of information (azure commands and powershell commands) is rather new to me and I am just looking for the easiest solution where I can create a task (within Azure) to restart our web application once per day. Any suggestions or guidance would be greatly appreciated.

Thank you!

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
5,666 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Manu Philip 16,576 Reputation points MVP

    Have you also tried with other similar cli commands

    az appservice web restart --resource-group xxx --name xxx

    --please don't forget to upvote and Accept as answer if the reply is helpful--

  2. VenkateshDodda-MSFT 12,091 Reputation points Microsoft Employee

    Austin Anderson Thanks for reaching out to Microsoft Q&A, apologize for any inconvenience caused on this.

    Based on the shared information, I have understood that you are trying to restart webapp using PowerShell based web jobs and it is failing with below error message.

    The term 'Restart-AzWebApp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

    • The above error occurred since by default the Azure PowerShell (Az) Cmdlets modules won't be available/installed on the app service worker instance.
    • Also, you won't be able to install the required PowerShell module since installation required to change the execution policy which needs admin privilege.

    Alternatively, to fulfill your requirement you can upload the required PowerShell modules to app service from local machine by creating a folder under (site\wwwroot) as shown below.


    and thereby using the below cmdlet to point your web job script to those PowerShell modules.

    Command : $Env:PSModulePath = $Env:PSModulePath+";C:\home\site\wwwroot"

    To test the above, I have created a created a scheduled PowerShell based web job in one of the windows webapp assigned with managed identity and respective permissions to authenticate and to perform the stop operation on another webapp that is running in same resource group.

    Script inside the webjob :

    $Env:PSModulePath = $Env:PSModulePath+";C:\home\site\wwwroot"
    Connect-AzAccount -Identity
    $status= Get-AzWebApp -ResourceGroupName '<ResourceGroupName>' -Name '<WebAppName>'
    if ( $status.State -eq 'Running'){
        stop-AzWebApp -ResourceGroupName '<ResourceGroupName>' -Name '<WebAppName>'

    Feel free to reach back to me if you have any further questions on this.

  3. TP 45,496 Reputation points

    Hi Austin,

    I recommend you use Azure Automation Runbook for this. It only takes a minute to set up, however, if you've never used it before and are thus unfamiliar, figure more like 10-15 minutes to slowly walk through each step. Basic steps to set it up below.

    Create Automation Account (you can skip this if you already have one):

    Give it resource group and name and accept defaults for the rest.

    Assign Role to Managed Identity

    Navigate to your automation account -- Account Settings -- Identity blade, click Azure role assignments. Click Add role assignment, select Scope: Resource group, Resource group: <resource group for webapp>, Role: Contributor, click Save.

    Create Runbook

    In your automation account -- Process Automation -- Runbooks blade, click Create a runbook. Give it a Name, Runbook type: PowerShell, Runtime version: 5.1, click Create. In the editor pane on right, paste in code similar to below sample (don't forget to update with your resource group and webapp names):

    Connect-AzAccount -Identity
    Restart-AzWebApp -ResourceGroupName MyResourceGroupName -Name MyWebAppName

    Click Publish. Alternatively you may click Test pane, run a test, close the test pane, then Publish.

    Click Resources -- Schedules blade. Click Add a schedule, Link a schedule to your runbook, Add a schedule. Give it a Name, set the time you want it to run each day, click Recurring, change the dropdown to Day, click Create.

    Please review pricing for Azure Automation. In your case you should fall within the free included units since the daily restarts shouldn't take up much run time.

    Automation pricing

    Please click Accept Answer if the above was useful. If something is unclear please add a comment below.



    0 comments No comments