Swap Slots in Azure Web Site when there are 2 or more staging slots using Azure PowerShell

 

Most of you would be aware of the Deployment Slots in Azure Web Sites. For those who are not familiar, 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/.

 

The swapping can be achieved easily using the Azure Management Portal using the 'SWAP' option under 'DASHBOARD' of the website/slots. It is also discussed in https://azure.microsoft.com/en-us/documentation/articles/web-sites-staged-publishing/#Swap.

 

But there are many instances where people want to make use of the powerful Azure PowerShell cmdlets to swap the slots.

 

Switch-AzureWebsiteSlot is the cmdlet to swap between the slots and the actual production environment. Below are some examples.

 

When there is only one slot and you want to swap the slot with the production environment, the example can be as simple as:

Switch-AzureWebsiteSlot –Name <AzureWebsiteName>

 

When there are two or more slots and you want to swap between the slots, the example can be as simple as:

Switch-AzureWebsiteSlot –Name <AzureWebsiteName> -Slot1 <slotName> -Slot2 <slotName>

 

But when there are two or more slots and you want to swap between one of the slot and the production environment, and you provided only one slot name in the syntax as below:

Switch-AzureWebsiteSlot –Name <AzureWebsiteName> -Slot1 <slotName>

 

It will then throw error like "The website has more than 2 slots you must specify which ones to swap" as shown below

 

 

PS C:\> Switch-AzureWebsiteSlot -Name "hkrishaspnet" -Slot1 "slot1"

Switch-AzureWebsiteSlot : The website has more than 2 slots you must specify which ones to swap

At line:1 char:1

+ Switch-AzureWebsiteSlot -Name "hkrishaspnet" -Slot1 "slot1"

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : CloseError: (:) [Switch-AzureWebsiteSlot], PSInvalidOperationException

+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Websites.SwitchAzureWebsiteSlotCommand

 

 

 

In this case, it is necessary to provide the slot name for the actual/production environment which is 'Production' . The syntax would be something like below:

Switch-AzureWebsiteSlot –Name <AzureWebsiteName> -Slot1 'Production' -Slot2 <slotName>

 

 

This will swap the corresponding slot content with the production slot of the Azure Web Site.