azure - 如何检查特定的 Azure ARM 资源是否存在,如果不存在则使用 PowerShell 创建它

标签 azure powershell

我正在尝试检查特定的图库图像版本是否存在,如果不存在,则创建图像版本。

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/

相关文章:

azure - 为什么 Azure Databricks 中未启用 SQL 终结点?

powershell - 在Powershell中将输出写入文件时出现问题

powershell - PowerShell组并在同一表中排序

c# - 如何在C#中将参数添加到powershellinstance

powershell - Azure WebJob - 将处理时间限制为特定时间

powershell - 按文件名查找并替换给定日期范围内文件中的字符串

azure - 当用户未在 Azure AD B2C 中注册时如何禁用验证码电子邮件

git - Azure DevOps错误: Git checkout failed with exit code: 1

.net - Visual Studio 检测旧的 Azure Functions 核心工具版本

azure - 性能计数器 "Internet Information Services Global"在 Azure Web 角色中不可用?