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:

Here is the script:

 1<# 
 2    .SYNOPSIS  
 3        Start or Stop all WebApps (Websites) in your subscription. 
 4 
 5    .DESCRIPTION 
 6        Runbook which allows you to start/stop all WebApps (Websites) in your subscription. 
 7 
 8    .PARAMETER Stop 
 9        If set to true: stop the WebApps (Websites). Otherwise start the WebApps (Websites) 
10 
11    .PARAMETER CredentialAssetName 
12        The name of a working AutomationPSCredential 
13         
14    .NOTES 
15        AUTHOR: Carlos Mendible 
16        LASTEDIT: June 2, 2016 
17#> 
18Workflow Stop-Start-All-WebApps  
19{ 
20    # Parameters 
21    Param( 
22        [Parameter (Mandatory= $true)] 
23        [bool]$Stop, 
24         
25        [Parameter (Mandatory= $true)] 
26        [string]$CredentialAssetName 
27       )   
28        
29    #The name of the Automation Credential Asset this runbook will use to authenticate to Azure. 
30    $CredentialAssetName = $CredentialAssetName; 
31     
32    #Get the credential with the above name from the Automation Asset store 
33    $Cred = Get-AutomationPSCredential -Name $CredentialAssetName 
34    if(!$Cred) { 
35        Throw "Could not find an Automation Credential Asset named '${CredentialAssetName}'. Make sure you have created one in this Automation Account." 
36    } 
37 
38    #Connect to your Azure Account        
39    Add-AzureRmAccount -Credential $Cred 
40    Add-AzureAccount -Credential $Cred 
41     
42    $status = 'Stopped' 
43    if ($Stop) 
44    { 
45        $status = 'Running' 
46    } 
47 
48    # Get Running WebApps (Websites) 
49    $websites = Get-AzureWebsite | where-object -FilterScript{$_.state -eq $status } 
50     
51    foreach -parallel ($website In $websites) 
52    { 
53        if ($Stop) 
54        { 
55            $result = Stop-AzureWebsite $website.Name 
56            if($result) 
57            { 
58                Write-Output "- $($website.Name) did not shutdown successfully" 
59            } 
60            else 
61            { 
62                Write-Output "+ $($website.Name) shutdown successfully" 
63            } 
64        } 
65        else 
66        { 
67            $result = Start-AzureWebsite $website.Name 
68            if($result) 
69            { 
70                Write-Output "- $($website.Name) did not start successfully" 
71            } 
72            else 
73            { 
74                Write-Output "+ $($website.Name) started successfully" 
75            } 
76        }  
77    }     
78}

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!


Backup your Team Services Git Repositories with VSTS Vault
Remove RavenDb's API from Swagger
comments powered by Disqus