Azure App Services: Get Deployment Slots using PowerShell
In this blog I will share one way to get Deployment Slots for a single App Service or all App Services using PowerShell.
My blogs have relatively simple, and sometimes complex, examples and I’m hoping that you will be able to tailor them to your need or use them in your own scripts.
Prerequisites
- Install Azure PowerShell if you haven’t already.
- Install Azure Active Directory PowerShell module if you haven’t already.
Introduction
The goal of this blog is to show one way to accomplish a task. It is not to show how to write a perfect script, the perfect solution to a challenge or the perfect process to accomplish a task.
Using PowerShell, I will show you how to get deployment slots for:
- A single App Service.
- All App Services.
I will also show, for demonstration, how to display PowerShell cmdlet output in a table format.
PowerShell Cmdlets
- Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
- Set-AzContext Sets the tenant, subscription, and environment for cmdlets to use in the current session.
- Get-AzWebApp Gets Azure Web Apps in the specified resource group.
- Get-AzWebAppSlot Gets an Azure Web App slot.
Sign in to Azure
- Sign in to Azure. If you have multiple subscriptions or tenants, make sure to sign in to the correct subscription.
- You can use Set-AzContext to set the tenant, subscription, and environment for cmdlets to use in the current session.
1Connect-AzAccount -Subscription "aa1111a1-1111-1a1a-11a1-1111a1a1a1a1"
Get Deployment Slots for a single App Service
- Get-AzWebAppSlot cmdlet is used to get the Web App slots.
123456789101112131415$WebAppSlots = Get-AzWebAppSlot -Name "app1-saad-dev" -ResourceGroupName "rg-app1-saad-dev" -ErrorAction SilentlyContinue$OutTable = @() # Initialize an array variable to save outputforeach ($WebAppSlot in $WebAppSlots) {# Define table columns$Result = "" | Select RepositorySiteName, Name, DefaultHostName, HttpsOnly, State$Result.RepositorySiteName = $WebAppSlot.RepositorySiteName$Result.Name = $WebAppSlot.Name$Result.DefaultHostName = $WebAppSlot.DefaultHostName$Result.HttpsOnly = $WebAppSlot.HttpsOnly$Result.State = $WebAppSlot.State$OutTable += $Result}Write-Host ""# Display $OutTable$OutTable | Format-Table
Get Deployment Slots for all App Services
- Use Get-AzWebApp to get a list of all App Services in the subscription.
- Initialize a variable, $OutTable, to save output.
- (a) For each App Service, (b) use Get-AzWebAppSlot to get a list of the App Service available slots.
- If the App does not have slots, add “No slots found” to $OutTable.
- For each App Service slot, add the required information to $OutTable.
- Display $OutTable.
12345678910111213141516171819202122232425262728293031323334$WebApps = Get-AzWebApp | Sort-Object Name # 1. Get a list of all Web Apps in the subscription$OutTable = @() # 2. Initialize an array variable to save outputWrite-Host "" # display empty lineforeach ($WebApp in $WebApps) { # 3. (a) For each App ServiceWrite-Host ("Getting slot configuration for [" + $WebApp.Name + "]")# 3. (b) use Get-AzWebAppSlot to get a list of the App Service available slots$WebAppSlots = Get-AzWebAppSlot -Name $WebApp.Name -ResourceGroupName $WebApp.ResourceGroup -ErrorAction SilentlyContinueif ($WebAppSlots -eq $null) { # 4. If the App does not have slots, add "No slots found" to $OutTable$Result = "" | Select WebApp, Name, DefaultHostName, HttpsOnly, State, AlwaysOn$Result.WebApp = $WebApp.Name$Result.Name = "No slots found"$Result.DefaultHostName = ""$Result.HttpsOnly = ""$Result.State = ""$Result.AlwaysOn = ""$OutTable += $Result}else {# 5. For each App Service slot, add the required information to $OutTable.foreach ($WebAppSlot in $WebAppSlots) {$Result = "" | Select WebApp, Name, DefaultHostName, HttpsOnly, State, AlwaysOn$Result.WebApp = $WebAppSlot.RepositorySiteName$Result.Name = $WebAppSlot.Name$Result.DefaultHostName = $WebAppSlot.DefaultHostName$Result.HttpsOnly = $WebAppSlot.HttpsOnly$Result.State = $WebAppSlot.State$Result.AlwaysOn = $WebAppSlot.SiteConfig.AlwaysOn$OutTable += $Result}}}Write-Host ""# 6. Display $OutTable$OutTable | Format-Table
Conclusion
In this blog we explored how to get Azure App Services Slots using PowerShell. We also explored how to output PowerShell cmdlet result as a table.
Did you find this blog easy to follow and helpful to you? Let me know in the comments below.
Disclaimer
Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
There's no comments