You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell创建Azure Function App时如何避免获取应用服务计划步骤

问题分析

New-AzFunctionApp cmdlet的内置逻辑要求验证指定的App Service Plan是否存在,因此即使你提前创建了Plan,它依然会自动调用Get-AzAppServicePlan来确认Plan的状态。-NoWait参数仅控制Function App本身的创建是否异步,无法跳过Plan的验证步骤。

解决方案

方法1:直接传入App Service Plan对象(推荐)

将创建好的App Service Plan保存到变量中,直接把该对象传递给New-AzFunctionApp的-Plan参数,cmdlet会直接使用已有的对象,无需再执行获取操作:

$functionAppName = "funcAppName"
$resourceGroupName = "RESGroup"
$appLocation = "region"
$storageName = "stgAccName"

# Connect to Azure 
$secret = "xxx"
$appID = "xxx"
$TenantId = "xxx"
$SecurePassword = ConvertTo-SecureString $secret -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $appID, $SecurePassword

Connect-AzAccount -ServicePrincipal -TenantID $TenantId -Credential $credentials

# 创建App Service Plan并保存到变量
$appServicePlan = New-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $functionAppName -Location $appLocation -Tier "Basic"
# 直接传入Plan对象创建Function App
New-AzFunctionApp $functionAppName -ResourceGroupName $resourceGroupName -Plan $appServicePlan -Runtime Node -StorageAccountName $storageName -FunctionsVersion 4 -OS Windows

方法2:使用New-AzResource直接创建(高级场景)

如果需要完全绕过New-AzFunctionApp的内置验证,可以使用New-AzResource直接构造Function App的资源属性,但这种方式需要手动指定所有必要的配置,示例如下:

$functionAppName = "funcAppName"
$resourceGroupName = "RESGroup"
$appLocation = "region"
$storageName = "stgAccName"
# 替换为你的订阅ID
$subscriptionId = "你的订阅ID"
$planId = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Web/serverFarms/$functionAppName"

# Connect to Azure 
$secret = "xxx"
$appID = "xxx"
$TenantId = "xxx"
$SecurePassword = ConvertTo-SecureString $secret -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $appID, $SecurePassword

Connect-AzAccount -ServicePrincipal -TenantID $TenantId -Credential $credentials

# 创建App Service Plan
New-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $functionAppName -Location $appLocation -Tier "Basic"

# 获取存储账户密钥
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageName)[0].Value
$storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=$storageName;AccountKey=$storageAccountKey;EndpointSuffix=core.windows.net"

# 直接创建Function App资源
New-AzResource -ResourceGroupName $resourceGroupName `
    -ResourceType "Microsoft.Web/sites" `
    -Name $functionAppName `
    -Location $appLocation `
    -Properties @{
        serverFarmId = $planId
        siteConfig = @{
            appSettings = @(
                @{name = "FUNCTIONS_WORKER_RUNTIME"; value = "node"},
                @{name = "FUNCTIONS_EXTENSION_VERSION"; value = "~4"},
                @{name = "AzureWebJobsStorage"; value = $storageConnectionString},
                @{name = "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING"; value = $storageConnectionString}
            )
        }
    } `
    -Force

内容的提问来源于stack exchange,提问作者suraj salunkhe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 11:21:11