在Powershell中设置AzureRmWebApp更改不会出现在Azure门户中

在Powershell中设置AzureRmWebApp更改不会出现在Azure门户中,azure,azure-web-app-service,azure-powershell,Azure,Azure Web App Service,Azure Powershell,我正在尝试通过Powershell将虚拟应用程序添加到Azure网站。虽然脚本运行时没有错误,但我的更改不会显示在portal.azure.com中。是否需要在最后运行另一个命令来持久化更改 $subscriptionId = "secret" $websiteName = "MyWebsite" $resourceGroupName = "MyResourceGroup" $siteName = "Test" Get-Module AzureRM Login-AzureRmAccount S

我正在尝试通过Powershell将虚拟应用程序添加到Azure网站。虽然脚本运行时没有错误,但我的更改不会显示在portal.azure.com中。是否需要在最后运行另一个命令来持久化更改

$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

尝试使用以下命令添加虚拟应用程序

$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
我在实验室测试,它对我有效


有关这方面的更多信息,请参阅和。

以沃尔特的答案为基础,使其工作正常。要将新虚拟应用程序附加到现有虚拟应用程序,请运行以下代码:

# 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

这似乎是可行的,但我需要附加一个虚拟应用程序,而不是替换现有的应用程序。我试图附加到Get-AzureRmWebApp的结果中,但仍然得到“缺少默认应用程序”/。我找到了答案;请参阅下面的答案。感谢您的指导!