azure - 通过 PowerShell 在消耗计划上创建 Azure 函数失败

标签 azure powershell azure-functions azure-sdk

我正在尝试通过 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\4.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

最佳答案

如本 MS Doc1 中所示和 MS Doc2 , 消费计划不需要PlanName,也不需要在消费模式下为功能应用创建应用服务计划。

这意味着,它将按照上述引用文献中的描述自动创建消费应用服务计划。

我已按照您的代码进行重现和解决,但没有给出计划名称,如下面的解决方法所示:

enter image description here

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
}

结果:

enter image description here

更新的答案:

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

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

enter image description here

关于azure - 通过 PowerShell 在消耗计划上创建 Azure 函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71388162/

相关文章:

asp.net - Azure 网站上的 REST API 上的 400.0 错误请求

azure - 更改执行策略并启用 PowerShell 脚本的执行

c# - 在同一域中路由 API 项目和 Web 应用程序项目 (Azure)

powershell - 表达式只允许作为管道的第一个元素

c# - Azure 函数当时仅具有最大并发数

azure - 我们如何防止 azure 函数处理已经处理过的 blob?

azure - 尝试通过 Active Directory 访问 Azure Databricks API 时出现错误 403 用户未授权

powershell - 读取 CSV 并在 PowerShell 中循环

java - 如何使用PowerShell脚本在Windows上启动本地neo4j服务器?

azure - 将进程内 COM DLL 与 Azure Function 结合使用