如何检查特定Azure ARM资源是否存在,如果不存在,如何使用PowerShell创建它
我正在尝试检查是否存在特定的库图像版本,如果不存在,请创建图像版本如何检查特定Azure ARM资源是否存在,如果不存在,如何使用PowerShell创建它,azure,powershell,Azure,Powershell,我正在尝试检查是否存在特定的库图像版本,如果不存在,请创建图像版本 Write-Host "Checking the image version already exists or not..................." $VersionReqName=(Get-AzGalleryImageVersion -ResourceGroupName $rgname -GalleryName $galName -GalleryImageDefinitionName $imag
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
}
您可以使用
-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
}