我正在尝试检查特定的图库图像版本是否存在,如果不存在,则创建图像版本。
Write-Host "Checking the image version already exists or not..................."
$VersionReqName=(Get-AzGalleryImageVersion -ResourceGroupName $rgname -GalleryName $galName -GalleryImageDefinitionName $imageDefinitionName -GalleryImageVersionName $versionName).Name
if($VersionReqName -eq $versionName)
{
Write-Host "The image version which has been requested in this pipeline task already exists, no action is taken, goodbye!!!" -ForegroundColor Green
}
else
{
Write-Host "Creating and replicating the new image, it may take hours depending on the size of the image and network speed"
$ImageVersion=New-AzGalleryImageVersion -ResourceGroupName $rgname -GalleryName $galName -GalleryImageDefinitionName $imageDefinitionName -GalleryImageVersionName $versionName -Location $location -Source $sourceImageId -PublishingProfileEndOfLifeDate $endOfLifeDate -TargetRegion $targetRegions
############################## Publish new Image Gallery #######################
Write-Host "The Gallery Image Version is ready please find below" -ForegroundColor "Green"
$ImageVersion
}
但是,我收到以下错误。
Checking the image version already exists or not...................
##[error]The Resource 'Microsoft.Compute/galleries/WVDImageGallery3/images/WVDBaseImageDefinition1/versions/2020.09.12' under resource group 'IMAGEGALLERYRG' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix
ErrorCode: ResourceNotFound
ErrorMessage: The Resource 'Microsoft.Compute/galleries/WVDImageGallery3/images/WVDBaseImageDefinition1/versions/2020.09.12' under resource group 'IMAGEGALLERYRG' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix
ErrorTarget:
StatusCode: 404
ReasonPhrase: Not Found
OperationID : c5ec1aec-9ec9-4287-ba80-8ad4d689945d
##[error]PowerShell exited with code '1'.
这实际上是正确的,但我想以这样的方式编写代码,就好像 Azure ARM 资源不存在一样,如果我收到错误代码 ResourceNotFound,它应该创建新资源。有人可以帮忙吗?
最佳答案
您可以使用-ErrorAction SilentlyContinue
。
类似这样的事情:
Write-Host "Checking the image version already exists or not..................."
if(Get-AzGalleryImageVersion -ResourceGroupName $rgname -GalleryName $galName -GalleryImageDefinitionName $imageDefinitionName -GalleryImageVersionName $versionName -ErrorAction SilentlyContinue)
{
Write-Host "The image version which has been requested in this pipeline task already exists, no action is taken, goodbye!!!" -ForegroundColor Green
}
else
{
Write-Host "Creating and replicating the new image, it may take hours depending on the size of the image and network speed"
$ImageVersion=New-AzGalleryImageVersion -ResourceGroupName $rgname -GalleryName $galName -GalleryImageDefinitionName $imageDefinitionName -GalleryImageVersionName $versionName -Location $location -Source $sourceImageId -PublishingProfileEndOfLifeDate $endOfLifeDate -TargetRegion $targetRegions
############################## Publish new Image Gallery #######################
Write-Host "The Gallery Image Version is ready please find below" -ForegroundColor "Green"
$ImageVersion
}
关于azure - 如何检查特定的 Azure ARM 资源是否存在,如果不存在则使用 PowerShell 创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63858281/