2
votes

I am trying to add a virtual application to an Azure website via Powershell. Though the script runs without errors, my changes don't appear in portal.azure.com. Is there another command I need to run at the end to persist the changes?

$subscriptionId = "secret"
$websiteName = "MyWebsite"
$resourceGroupName = "MyResourceGroup"
$siteName = "Test"

Get-Module AzureRM
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$website = Get-AzureRmWebApp -Name $websiteName -ResourceGroupName $resourceGroupName
$VDApp = New-Object Microsoft.Azure.Management.WebSites.Models.VirtualApplication
$VDApp.VirtualPath = "/$siteName"
$VDApp.PhysicalPath = "site\$siteName"
$VDApp.PreloadEnabled ="YES"
$website.siteconfig.VirtualApplications.Add($VDApp)
$website | Set-AzureRmWebApp -ResourceGroupName $resourceGroupName
2

2 Answers

0
votes

Try to use the following commands to add a virtual application.

$resourceGroupName = ""
$websiteName = ""
$WebAppApiVersion = "2015-08-01"
# Example call: SetWebAppConfig MyResourceGroup MySite $ConfigObject
Function SetWebAppConfig($ResourceGroupName, $websiteName, $ConfigObject)
{
    Set-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/Config -Name $websiteName/web -PropertyObject $ConfigObject -ApiVersion $WebAppApiVersion -Force
}
Write-Host "Set a virtual application"
$props=@{
    virtualApplications = @(
        @{ virtualPath = "/"; physicalPath = "site\wwwroot" },
        @{ virtualPath = "/bar"; physicalPath = "site\wwwroot" }
    )
}
SetWebAppConfig $ResourceGroupName $websiteName $props

I test in my lab, it works for me.

enter image description here

More information about this, see here and here.

0
votes

Got it working using Walter's answer as a base. To append a new virtual application to the existing ones, run this code:

# Example call: SetWebAppConfig MyResourceGroup MySite $ConfigObject
# https://github.com/davidebbo/AzureWebsitesSamples/blob/b2a3d67a90b7b2d673d68dab553f82e015333d10/PowerShell/HelperFunctions.ps1#L84-L87
Function SetWebAppConfig($ResourceGroupName, $SiteName, $ConfigObject)
{
    $WebAppApiVersion = "2015-08-01"
    Set-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/Config -Name $SiteName/web -PropertyObject $ConfigObject -ApiVersion $WebAppApiVersion -Force
}

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId $subscriptionId

$website = Get-AzureRmWebApp -ResourceGroupName $resourceGroupName -Name $websiteName
$virtualApplications = $website.SiteConfig.VirtualApplications

$hash = New-Object System.Collections.ArrayList

for ($j = 0; $j -lt $virtualApplications.count; $j++) {
    $entry = @{ virtualPath = $virtualApplications[$j].VirtualPath; physicalPath = $virtualApplications[$j].PhysicalPath }
    $hash.add($entry)
}
$newEntry = @{ virtualPath = "/$siteName"; physicalPath = "site\$siteName" }
$hash.add($newEntry)


$hashProps.VirtualApplications
$virtualApps = $website.SiteConfig.VirtualApplications.Add(@{ virtualPath = "/$siteName"; physicalPath = "site\$siteName" })
$props=@{
    virtualApplications = $hash.ToArray()
}

SetWebAppConfig $resourceGroupName $websiteName $props