如何通过 azure devops 发布管道中的 powershell 任务创建 azure 资源?

How to create a azure resource through powershell task in azure devops release pipeline?

我想通过 powershell 任务创建应用服务计划(消费)。为此,我使用了 Azure Powershell 任务,我的代码是:

[cmdletbinding()]
    param (
        $AppServicePlanLocation, 
        $AppServicePlanResourceGroupName,
        $AppServicePlan_Name
    )

$location = $AppServicePlanLocation
$resourceGroupName = $AppServicePlanResourceGroupName
$appServicePlanName = $AppServicePlan_Name

Write-Host "SafeCreateAppServicePlan.Parameter:location: $location"
Write-Host "SafeCreateAppServicePlan.Parameter:resourceGroupName: $resourceGroupName"
Write-Host "SafeCreateAppServicePlan.Parameter:appServicePlanName: $appServicePlanName"

$SkuName = "Y1"
$SkuTier = "Dynamic"
$WebAppApiVersion = "2015-08-01"

$fullObject = @{
    location = $location
    sku = @{
        name = $SkuName
        tier = $SkuTier
    }
}

Write-Host "Ensuring the $appServicePlanName app service plan exists"
$plan = Get-AzureRmAppServicePlan -Name $appServicePlanName -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
if(-not $plan) {
    Write-Host "Creating $appServicePlanName app service plan"
    New-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/serverfarms -Name $appServicePlanName -IsFullObject -PropertyObject $fullObject -ApiVersion $WebAppApiVersion -Force
}
else {
    Write-Host "$appServicePlanName app service plan already exists"   
}

但是报错: 术语 'Get-AzureRmAppServicePlan' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

Azure Powershell 任务无法识别 Get-AzureRmAppServicePlan 命令。

注意:我没有用过简单的powershell任务,我用过Azure Powershell任务。

您没有提及您正在使用的 Azure PowerShell 任务的任务版本,或者代理 运行 在 Hosted\Private 中的上下文。如果模块可用,这两者都会起作用。

可以检查 AzureRM.Websites 模块是否可用。

$ Get-Module Azure* -list | Select-Object Name,Version,Path

您可能需要手动加载模块,或指定旧版本的 Azure PowerShell 任务,因为在使用 newer version of the task.

时 AzureRM 模块不会自动可用。

如果您使用的是最新版本的任务 - 将所有 *AzureRM* 命令替换为 *Az*(或 enable backwards compatibility),因为这是受支持的 Azure Powershell 模块,当前( AzureRM 已贬值且不再受支持)。

https://docs.microsoft.com/en-us/powershell/azure/migrate-from-azurerm-to-az?view=azps-3.3.0

ps。如果您使用的是以前版本的任务 - AzureRM 应该可以