Stop or Start all WebApps in your Azure subscription

by Carlos Mendible on 05 Jun 2016 » Azure, DevOps

Stop or Start all WebApps in your Azure subscription with Stop-Start-All-WebApps.ps1 a simple PowerShell workflow runbook that will help you automate the process of stopping or starting every single WebApp (Website) you’ve deployed.

The script receives two required parameters:

  • Stop: If set to true stop the WebApps (Websites) otherwise start the WebApps (Websites)
  • CredentialAssetName: The name of a valid Credential Asset (AutomationPSCredential) you must have previously configured.

As you can see below, the script is quite simple and uses the following PowerShell Activities and Cmdlets:

Type Name Description
Activity Get-AutomationPSCredential Gets a credential to use in a runbook or DSC configuration. Returns a System.Management.Automation.PSCredential object.
Cmdlet Add-AzureAccount Adds an authenticated account to use for Resource Manager cmdlet requests.
Cmdlet Add-AzureRmAccount Adds the Azure account to Windows PowerShell
Cmdlet Get-AzureWebsite Gets Azure websites in the current subscription
Cmdlet Stop-AzureWebsite Stops the specified webiste
Cmdlet Start-AzureWebsite Starts the specified webiste

Here is the script:

<# 
    .SYNOPSIS  
        Start or Stop all WebApps (Websites) in your subscription. 
 
    .DESCRIPTION 
        Runbook which allows you to start/stop all WebApps (Websites) in your subscription. 
 
    .PARAMETER Stop 
        If set to true: stop the WebApps (Websites). Otherwise start the WebApps (Websites) 
 
    .PARAMETER CredentialAssetName 
        The name of a working AutomationPSCredential 
         
    .NOTES 
        AUTHOR: Carlos Mendible 
        LASTEDIT: June 2, 2016 
#> 
Workflow Stop-Start-All-WebApps  
{ 
    # Parameters 
    Param( 
        [Parameter (Mandatory= $true)] 
        [bool]$Stop, 
         
        [Parameter (Mandatory= $true)] 
        [string]$CredentialAssetName 
       )   
        
    #The name of the Automation Credential Asset this runbook will use to authenticate to Azure. 
    $CredentialAssetName = $CredentialAssetName; 
     
    #Get the credential with the above name from the Automation Asset store 
    $Cred = Get-AutomationPSCredential -Name $CredentialAssetName 
    if(!$Cred) { 
        Throw "Could not find an Automation Credential Asset named '${CredentialAssetName}'. Make sure you have created one in this Automation Account." 
    } 
 
    #Connect to your Azure Account        
    Add-AzureRmAccount -Credential $Cred 
    Add-AzureAccount -Credential $Cred 
     
    $status = 'Stopped' 
    if ($Stop) 
    { 
        $status = 'Running' 
    } 
 
    # Get Running WebApps (Websites) 
    $websites = Get-AzureWebsite | where-object -FilterScript{$_.state -eq $status } 
     
    foreach -parallel ($website In $websites) 
    { 
        if ($Stop) 
        { 
            $result = Stop-AzureWebsite $website.Name 
            if($result) 
            { 
                Write-Output "- $($website.Name) did not shutdown successfully" 
            } 
            else 
            { 
                Write-Output "+ $($website.Name) shutdown successfully" 
            } 
        } 
        else 
        { 
            $result = Start-AzureWebsite $website.Name 
            if($result) 
            { 
                Write-Output "- $($website.Name) did not start successfully" 
            } 
            else 
            { 
                Write-Output "+ $($website.Name) started successfully" 
            } 
        }  
    }     
}

You can download the script from the Technet Script Gallery, import it as a runbook for your automation account in the Azure portal or simply copy the code from this post.

Hope it helps!