PowerShell How-To

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.

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:

  1. Retrieving the Microsoft.Azure.Management.WebSites.Models.Site object.
  2. Creating a Microsoft.Azure.Management.WebSites.Models.VirtualApplication object.
  3. 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.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular