The process of creating and assigning a virtual directory to an Azure Web App involves the following 3 steps:
1. Retrieve the Microsoft.Azure.Management.WebSites.Models.Site object.
2. Create Microsoft.Azure.Management.WebSites.Models.VirtualApplication object.
3. Assign VirtualApplication object to Site object.
Creating a Virtual Directory in Azure Web App with PowerShell
There's a bit of complexity with .NET objects when creating a virtual directory, so to overcome this and create a script, follow the steps below:
Firstly, ensure that you have installed the latest AzureRm PowerShell module (Install-Module -Name AzureRm). After downloading the latest AzureRm PowerShell module to your machine, the next step is to retrieve the Web App to attach the virtual directory. You can accomplish this using Get-AzureRmWebApp by specifying its resource group and web app name.
$webApp = Get-AzureRmWebApp -Name $AzureWebAppName -ResourceGroupName $AzureResourceGroup
The next step involves creating a VirtualApplication object. This object has 3 properties of interest: VirtualPath, PhysicalPath, and PreloadEnabled (optional). The VirtualPath and PhysicalPath properties are visible in the Azure Web portal if you view the Application Settings of a Web app.
This VirtualApplication object is created from the Microsoft.Azure.Management.WebSites.Models.VirtualApplication class and sets the mentioned properties.
$virtApp = New-Object Microsoft.Azure.Management.WebSites.Models.VirtualApplication
$virtApp.VirtualPath = $VirtualPath
$virtApp.PhysicalPath = $PhysicalPath
$virtApp.PreloadEnabled = $PreloadEnabled
After creating the VirtualApplication object, the next step is to assign it to the Web App. The Microsoft.Azure.Management.WebSites.Models.Site object returned from Get-AzureRmWebApp contains a siteconfig property.
This project has a VirtualApplications property with an Add() method that accepts the Microsoft.Azure.Management.WebSites.Models.VirtualApplication object. Use this Add() method to pass the VirtualApplication object you just created to the Web App.
$null = $webApp.siteconfig.VirtualApplications.Add($virtApp)
So, you've just created a virtual directory and added it to the local Web App. The next step is to commit your changes to the Azure Web App using the Set-AzureRmWebApp command. Since the $webApp variable now has the attached virtual directory, you can overwrite the current file with the modified object.
Execute: Set-AzureRmWebApp -WebApp $webApp
Now that you've successfully built all the necessary code to attach a virtual directory to your Azure Web App, you can consolidate this code into a parameterized script. You no longer need to worry about these individual pieces of code. This script (or function) will be stored in the Azure Web repository.
The Mytour article provides guidance on creating a Virtual Directory in an Azure Web App using PowerShell.
PowerShell's capabilities are extensive, not only on Azure Web but also integrated into Windows 10. This allows users to perform a variety of tasks, such as backing up drivers on Windows 10 using PowerShell. If you have any doubts or questions, feel free to leave your comments below the article.
