Stop or Start all WebApps in your Azure subscription

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<# 
    .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!

Leave a Reply