Update Azure DevTest Labs VM expiration - Powershell - ERROR One of the following properties must be specified: customImageId, galleryImageReference.'

Jade Charles 1 Reputation point
2022-01-21T05:22:59.727+00:00

I've got some virtual machines in a DevTest Lab (DTL) that have been created from a Azure Compute Gallery image. During their setup they were given an expiry date and I've been asked to extend this by a stakeholder.

We've recently changed how our VMs are created from custom images with-in a single DTL to using a centralised Azure compute gallery. I did some searching and with our old VMs that were created with custom images, the below code worked fine, however with using the newly renamed compute galleries being used as bases for the VMs the code fails with the following error:

'Set-AzResource: MissingRequiredProperties : One of the following properties must be specified: customImageId, galleryImageReference.'

This LOOKS to be a bug in that the new AzResource method doesn't cater for the new Azure Compute Galleries. I can see from my resource properties that it includes a parameter called "sharedImageId" which has been populated and so I'm assuming it is meant to use that. I am quite new to all things Azure, so if anyone has any better suggestions or ideas that would be wonderful.

Code Below:

$subscriptionId = ‘<mySubscriptionID>’

$labResourceGroup = ‘<labResourceGroup>’

$labName = ‘<DTL-Name>’

$VmName = ‘virtualMachineName’

$expirationDate = ‘2022-05-31’

Log into your Azure account

Select-AzSubscription -SubscriptionId $subscriptionId

$VmResourceId = “subscriptions/$subscriptionId/resourcegroups/$labResourceGroup/providers/microsoft.devtestlab/labs/$labName/virtualmachines/$VmName”

$vm = Get-AzResource -ResourceId $VmResourceId -ExpandProperties

Get all the Vm properties

$VmProperties = $vm.Properties

Set the expirationDate property

$VmProperties | Add-Member -MemberType NoteProperty -Name expirationDate -Value $expirationDate -Force

Set-AzResource -ResourceId $VmResourceId -Properties $VmProperties -Force

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,586 questions
Azure DevTest Labs
Azure DevTest Labs
An Azure service that is used for provisioning development and test environments.
267 questions
{count} votes