【问题标题】:PowerShell Task not running the script in Azure Pipelines?PowerShell 任务未在 Azure Pipelines 中运行脚本?
【发布时间】:2022-01-05 15:14:35
【问题描述】:

我有一个 powershell 任务,用于运行涉及创建 azure 资源的脚本(例如:资源组、Azure Key Vault、Function App...)。当管道正在运行并到达部署阶段的 powershell 任务时,它会显示以下消息:

这里的问题,它说 Finishing:Powershell 但它没有执行脚本并且没有创建任何 azure 资源。

这是一个 powershell 脚本示例:

$vaultName = "key vault name"
$blobstorageName = "blob storage name"
$Location = "Location Name"
$resourceGroupName = "Resource Group Name"

try {

    #Creation of Resource Group
    $resourceGroup = Get-AzResourceGroup -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue 

    if($null -eq $resourceGroup)
    {
        New-AzResourceGroup -Name $resourceGroupName -Location $Location
    }

    else
    {
        Write-Host "The ResourceGroup with the name: $resourceGroupName already exists."
    }

    # Creation of Storage Account

    $checkBlobStorage = (Get-AzStorageAccountNameAvailability -Name $blobstorageName) | Select-Object NameAvailable
    if ($checkBlobStorage.NameAvailable)
    {
        New-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $blobstorageName -Location $Location -SkuName Standard_LRS -Kind StorageV2 -AccessTier Hot
    }

    else 
    {
        Write-Host "The name $blobStorageName is not available. Suggest a new globally unique name!"
    }


catch 
{

}

有人知道出了什么问题吗?我是否遗漏了 powershell 脚本中的某些内容(也许我无法从 azure devops 直接访问 azure 门户),或者可能缺少某些内容 Yaml 文件?

【问题讨论】:

  • “我在 powershell 脚本中遗漏了什么” - 是的,你有 0 错误处理 - 你只是吞下空的 catch{} 块引发的任何错误,所以你永远不会知道实际发生了什么.首先在catch 块中添加Write-Host "Error occurred: $_",看看它会把你带到哪里:)
  • @MathiasR.Jessen 你绝对是对的。感谢那。我收到以下错误:发生错误:“Get-AzResourceGroup”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。但是在这种情况下我该怎么办?
  • 使用调用Install-Module Az.Resources 的脚本向管道添加上一步。
  • @MathiasR.Jessen 我可以在顶部的同一脚本中添加 Install-Module Az.Resources 吗?是否也可以安装 Az 模块?
  • 试一试:)

标签: azure powershell azure-pipelines azure-pipelines-build-task azure-yaml-pipelines


【解决方案1】:

两大问题:

  1. 您似乎正在使用Powershell Task,它不是为与 Azure 通信而设计的。对于这种脚本,您应该使用 Azure Powershell task,因为它已经加载了正确的模块并准备好了身份验证。
  2. 您的脚本正在吞噬错误,因此它隐藏了问题所在。 not 捕获异常通常更有用;如果您的脚本出错,则让它出错,并让管道在其日志中向您显示发生了什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2020-10-05
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多