通过 PowerShell 在消费计划上创建 Azure 函数失败

Creating of Azure function on consumption plan via PowerShell fails

我正在尝试通过 PowerShell 创建 Azure 函数。

首先我成功使用基本计划

$azFunctionAppServiceObject = @{
   location = $iothubLocation
   sku = @{
        name = "B1"
        tier = "Basic"
   }
   kind = "functionapp"
   properties = @{
        reserved = "false"
        kind = "functionapp"
   }
}

$appServicePlan = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType "Microsoft.Web/serverfarms" -Name $appServicePlanName -ErrorAction SilentlyContinue

if (!$appServicePlan)
{
    $appServicePlan = New-AzResource -ResourceGroupName $resourceGroupName -ResourceType "Microsoft.Web/serverfarms" -Name $appServicePlanName -IsFullObject -PropertyObject $azFunctionAppServiceObject -Force
}
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -ErrorAction SilentlyContinue
if (!$storageAccount)
{
    Write-Output ">----------- Creating storage account"
    $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -SkuName Standard_LRS -Location $iothubLocation -Kind StorageV2 -AccessTier Hot
}

$trigger  = Get-AzFunctionApp -ResourceGroupName $resourceGroupName `
    -Name $FunctionAppName `

if (!$trigger)
{
    Write-Output ">----------- Creating trigger"

    New-AzFunctionApp -ResourceGroupName $resourceGroupName `
        -Name $FunctionAppName `
        -PlanName $appServicePlanName `
        -StorageAccount $blobStorageAccountName `
        -Runtime Dotnet `
        -FunctionsVersion 4 `
        -RuntimeVersion 6 `
        -OSType Windows
}

但是如果我将配置对象更改为:

$azFunctionAppServiceObject = @{
   location = $iothubLocation
   sku = @{
        name = "Y1"
        tier = "Dynamic"
   }
   kind = "functionapp"
   properties = @{
        reserved = "false"
        kind = "functionapp"
   }
}

我明白了

Az.Functions.internal\New-AzFunctionApp : The server responded with a Request Error, Status: Conflict At C:\Program Files\WindowsPowerShell\Modules\Az.Functions.0.1\custom\New-AzFunctionApp.ps1:528 char:17
+ ...             Az.Functions.internal\New-AzFunctionApp @PSBoundParameter ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ({ ResourceGroup...20190801.Site }:<>f__AnonymousType0`4) [New-AzFunctionApp_Create], RestException`1
    + FullyQualifiedErrorId : Conflict,Microsoft.Azure.PowerShell.Cmdlets.Functions.Cmdlets.NewAzFunctionApp_Create

使用 PowerShell 在消费计划上创建 Azure 函数时,您需要对 New-AzFunctionApp 调用使用不同的参数。查看 parameters,您可以跳过 -PlanName 参数,而是使用 -Location 参数。

有一个关于如何执行此操作的示例here

如此MS Doc1 and MS Doc2,消费计划不需要PlanName,也不需要在消费模式下为功能应用程序创建应用程序服务计划.

也就是说,它会按照上面的参考资料自动创建消费应用服务计划。

我已经按照您的代码重现并解决了问题,但没有给出计划名称,您可以在下面的解决方法中看到:

PowerShell 代码:

$iothubLocation = 'Southeast Asia'
$resourceGroupName = 'hariRG'
$blobStorageAccountName = 'krishfuncblob'
$FunctionAppName = 'krishfuncapp12059'
$subscriptionId = 'xxxx-xxxx-xxxx-xxxx-xxx'


#=================Creating Azure Resource Group===============
$resourceGroup = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $resourceGroupName }
if ($resourceGroup -eq $null)
{
  New-AzResourceGroup -Name $resourceGroupName -Location $location -force
}

#selecting default azure subscription by name
Select-AzSubscription -SubscriptionID $subscriptionId
Set-AzContext $subscriptionId


#========Creating Azure Storage Account========
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -ErrorAction SilentlyContinue


if (!$storageAccount)
{
    Write-Output ">----------- Creating storage account"
    $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -SkuName Standard_LRS -Location $iothubLocation -Kind StorageV2 -AccessTier Hot
}



#========Creating Azure Function========
$trigger  = Get-AzFunctionApp -ResourceGroupName $resourceGroupName -Name $FunctionAppName

if (!$trigger)
{
    Write-Output ">----------- Creating trigger"

    New-AzFunctionApp -ResourceGroupName $resourceGroupName `
        -Name $FunctionAppName `
        -StorageAccount $blobStorageAccountName `
        -Runtime Dotnet `
        -FunctionsVersion 4 `
        -RuntimeVersion 6 `
        -OSType Windows
}

结果:

更新答案:

Azure Portal 本身不允许在现有消费计划中创建另一个功能应用。

它只允许在现有的托管计划类型(应用服务计划和高级类型)中创建另一个功能应用程序,如下面的 Gif 图片所示: