6

Is there a way to add a Virtual Directory to an Azure Website using Powershell?

Background: we have a single solution with an ASP.Net MVC application and a Web.API projects. We are publishing the application into the root of an azure website, and the Web.API into a virtual directory under it.

This works well as long as you add the virtual directory manually in Azure portal. (Project settings do not propagate to the website settings for some reason).

As we are looking to automate the deployment process we would like to add the missing virtual directory using a Powershell script.

2
  • Did you ever find an answer to this? Commented Feb 27, 2015 at 17:03
  • 1
    @w69rdy has the greatest answer! Commented Mar 15, 2018 at 14:32

4 Answers 4

4

Looks like a valid (if somewhat complex) answer here:

The short of it is: You gotta use AzureResourceManager and work with a Microsoft.web/sites/config object to get and set Virtual Directories.

https://social.msdn.microsoft.com/Forums/azure/en-US/990f41fd-f8b6-43a0-b942-cef0308120b2/add-virtual-application-and-directory-to-an-azure-website-using-powershell?forum=windowsazurewebsitespreview

Sign up to request clarification or add additional context in comments.

Comments

4

I've just been struggling to do this recently, and found a simpler solution here:

 $website = Get-AzureRmWebApp -Name "myWebApp" -ResourceGroupName "myResourceGroup" 
 $VDApp = New-Object Microsoft.Azure.Management.WebSites.Models.VirtualApplication 
 $VDApp.VirtualPath = "/TEST" 
 $VDApp.PhysicalPath = "site\\TEST" 
 $VDApp.PreloadEnabled ="YES" 
 $website.siteconfig.VirtualApplications.Add($VDApp) 
 $website | Set-azureRmWebApp 

1 Comment

I tried using this solution in the inline powershell script of my Azure Release pipeline, but ran into errors (more details in the response below) until I mapped all of the AzureRM commands to Az commands and then things started working. Thanks @iain-ward for the starting point.
1

Based on Ben's answer:

$props = @{
    "virtualApplications" = @(
        @{
            "virtualPath"  = "/";
            "physicalPath" = "site\wwwroot";
        },
        @{
            "virtualPath"  = "/SOME_VIRTUAL_PATH";
            "physicalPath" = "site\wwwroot";
        }
    )
}

Set-AzureRmResource `
    -ResourceGroupName $resourceGroupName `
    -ResourceType "Microsoft.Web/sites/config" `
    -ResourceName "$appServiceName/web" `
    -PropertyObject $props `
    -ApiVersion "2015-08-01" `
    -Force

Comments

1

We had a similar situation as described by @divineops in the question, but we wanted to create a new virtual directory each time we built a new version of the application (Angular + asp.net web api) so that multiple versions of the applications are available for testing, each sitting inside it own versioned virtual directory.

So, we needed the powershell script to create the virtual directory each time the release pipeline in devops ran.

On setting up the script provided by @iain-ward, we first encountered an error in Azure DevOps release pipeline where it told us to run the Login-AzureRmAccount command.

On running this command with the necessary parameters (i.e. the credentials), we got the next error as:

Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.WebSites.WebSiteManagementClient' from assembly 'Microsoft.Azure.Management.Websites, Version=1.0.0.2, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.

To get past this error, I finally converted all the AzureRm commands to Az commands and below is the script that finally worked for me to accomplish this task of creation of a virtual directory from within a devops release pipeline:

$UserName = "<email>"
$Password = ConvertTo-SecureString "<password>" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
Login-AzAccount -Credential $Credential -SubscriptionName "<subscriptionName>" -TenantId "<tenantGuid>"
$website = Get-AzWebApp -Name "<appServiceName>" -ResourceGroupName "<resourceName>" 
$VDApp = New-Object Microsoft.Azure.Management.WebSites.Models.VirtualApplication 
$VDApp.VirtualPath = "/<virtualDirectory>" 
$VDApp.PhysicalPath = "site\\wwwroot\\<physicalPath>" 
$VDApp.PreloadEnabled ="NO" 
$website.siteconfig.VirtualApplications.Add($VDApp) 
$website | Set-AzWebApp 

All items in "<>" are placeholders that you need to replace with values that pertain to your account. Note - I had to specify the tenant id and the subscription id because my account was associated with multiples and the script was not able to choose which one to set as the current for the current script session.

Reference - It was based on this article that I decided to map all the AzureRm commands to Az commands as AzureRm is now outdated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.