Azure Powershell Script to list Sites / Staging / Deployment Slots for Web Hosting Plans

 

Before discussing about the Azure PowerShell Script, below is a brief about Web Hosting Plans and Deployment Slots in Azure Web Sites; if you are not using them already.

Web Hosting Plans gives you an option to isolate the web sites within the Resource Groups for the region. It provides efficiency in maintaining the resources for the specific sites with respect to scaling and management.

More on how to create web hosting plan and when to create web hosting plan can be found in these articles:

https://azure.microsoft.com/en-us/documentation/articles/azure-web-sites-web-hosting-plans-in-depth-overview/

https://azure.microsoft.com/blog/2014/04/04/introducing-web-hosting-plans-for-azure-web-sites/

 

Deployment Slots provide an option to deploy your changes to staging environment instead of directly moving the changes to the production environment. This helps you to validate your changes in the Azure Web Sites environment before you can swap the changes with the production environment. For details about the deployment slots, check https://azure.microsoft.com/en-us/documentation/articles/web-sites-staged-publishing/.

 

Now, when you create a new deployment slot in the new preview portal (https://portal.azure.com), by default, the deployment slot is created under the same web hosting plan as the web site web hosting plan. It is possible to change the web hosting plan for the deployment slot similar to the web site web hosting plan. 

NOTE: Changing the web hosting plan for the web site at a later stage does not change the web hosting plan for the deployment slot.

 

Here, while moving the staging site to 'testpowershellslot' web hosting plan for testing purpose, you would get a notification like "Successfully updated website hkrishaspnet(staging) to web hosting plan testpowershellslot". But when you look into the web hosting plan details, it will still show as 0 for 'testpowershellslot' web hosting plan.

 

 

NOTE: The web hosting plan displays only the web site web hosting plan and not that of the deployment slots. There is no easier way to check the deployment slots under specific web hosting plan. In case you are hosting tens and hundreds of sites and staging slots, then you should manually check each one of them to identify.

 

 

 

 

If you try to remove all the websites from the specific web hosting plan and try to delete the web hosting plan without removing/moving all the deployment slots associated with that web hosting plan then it would throw an error message "Failed to delete web hosting plan testpowershellslot: Server farm 'testpowershellslot' cannot be deleted because it has sites assigned to it".

 

 

This is where Azure PowerShell comes to your rescue to identify the sites/deployment slots under web hosting plan. To get this working, make sure you have installed and configured your subscription. For details, refer https://azure.microsoft.com/en-in/documentation/articles/install-configure-powershell/.

Once you have installed and added your subscription using Add-AzureAccount cmdlet, you should switch to Azure Resource Manager module using the cmdlet:

Switch-AzureMode -Name AzureResourceManager

 

Now run the below script to get the details of the web hosting plans for the different sites and deployment slots.

 

Login-AzureRmAccount

$Resource = Get-AzureRMResource

foreach ($item in $Resource) { if ($item.ResourceType -Match "Microsoft.Web/sites/slots") {   $siteProperties =(Get-AzureRMResource -Name $item.Name -ResourceGroupName $item.ResourceGroupName -ResourceType $item.ResourceType -ApiVersion 2014-04-01).Properties  write-host "WebHostingPlan " $siteProperties.ServerFarm " under slot " $item.Name ; }

elseif ($item.ResourceType -Match "Microsoft.Web/sites") {   $siteProperties =(Get-AzureRMResource -Name $item.Name -ResourceGroupName $item.ResourceGroupName -ResourceType $item.ResourceType -ApiVersion 2014-04-01).Properties  write-host "WebHostingPlan " $siteProperties.ServerFarm " under site " $item.Name ; } }

 

 

 

 

The PowerShell script can be modified to fetch any property of the site and use it as needed.