How To Create Virtual Directories in Azure Web Apps with PowerShell
Until Microsoft adds a native command that easily connects virtual directories to Azure Web apps, we're going to have to do it ourselves.
- By Adam Bertram
In the era before cloud computing, writing an entire article just to create an IIS virtual directory in PowerShell would not have been necessary. After all, just run the New-WebVirtualDirectory PowerShell command and be done with it! Nowadays, with the cloud reinventing just about every aspect of IT, things are done a little differently. Perhaps one day we'll have a native New-AzureWebVirtualDirectory command inside of the Azure PowerShell module. But until then, we'll just have to build our own. Here's how.
Creating and attaching a virtual directory to an Azure Web app consists of three steps:
- Retrieving the Microsoft.Azure.Management.WebSites.Models.Site object.
- Creating a Microsoft.Azure.Management.WebSites.Models.VirtualApplication object.
- Assigning the VirtualApplication object to the Site object.
Having to mess with .NET objects just to create a virtual directory doesn't sound like too much fun, so let's solve this problem once and create a script so we don't have to do this again.
First, ensure you have the latest AzureRm PowerShell module (Install-Module -Name AzureRm). Once that's downloaded, we then need to retrieve the Web app we'll be attaching the virtual directory to. We can use Get-AzureRmWebApp to do that by specifying the resource group it's in and the name of the web app itself.
$webApp = Get-AzureRmWebApp -Name $AzureWebAppName -ResourceGroupName $AzureResourceGroup
Next, we'll need to create a VirtualApplication object. This object has three properties we're concerned about; VirtualPath, PhysicalPath and, optionally, PreloadEnabled. The VirtualPath and PhysicalPath properties are what you can see displayed in the Azure Web portal if viewing the Application Settings section of a Web app.
This VirtualApplication object is created be instantiating it from the Microsoft.Azure.Management.WebSites.Models.VirtualApplication class and setting the properties that were just mentioned.
$virtApp = New-Object Microsoft.Azure.Management.WebSites.Models.VirtualApplication
$virtApp.VirtualPath = $VirtualPath
$virtApp.PhysicalPath = $PhysicalPath
$virtApp.PreloadEnabled = $PreloadEnabled
Once the VirtualApplication object is created, we'll then need to assign it to the Web app. The Microsoft.Azure.Management.WebSites.Models.Site object that's returned from Get-AzureRmWebApp contains a siteconfig property. This project contains a VirtualApplications property which has an Add() method that accepts a Microsoft.Azure.Management.WebSites.Models.VirtualApplication object. We can use this Add() method to pass our VirtualApplication object we just created to the Web app.
$null = $webApp.siteconfig.VirtualApplications.Add($virtApp)
We've now created the virtual directory and have added it to the Web app locally. Azure still has no idea we've done anything. We need to commit our change to the Azure Web app by using the Set-AzureRmWebApp command. Since our $webApp variable now has the virtual directory "attached" to it, we can overwrite our existing one with our newly modified object.
Set-AzureRmWebApp -WebApp $webApp
We're done! Now that we've got all of the code built to attach a virtual directory to an Azure Web app, we can roll all of this up into a parameterized script, so we don't have to worry about this code again. I've created an example of how to do this in my New-AzureWebAppVirtualDirectory.ps1 script as an example.
This script (or function, if you choose to implement it like that) should be in any Azure Web guru's arsenal. It's another great example of plugging holes in existing commercial PowerShell modules that may not have this functionality built in already.
Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.