【发布时间】: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