Azure / azure-devtestlab

Azure DevTestLab artifacts, scripts and samples
MIT License
458 stars 602 forks source link

How to update/extend expirationDate of an existing DevTest lab VM that already has one set? #776

Open slime-uk opened 3 years ago

slime-uk commented 3 years ago

Hi,

Searching the internet we found some articles regarding how to use Azure PowerShell to extend a date on a lab VM for expiry.

Examples:

  1. https://github.com/Azure-Samples/virtual-machines-powershell-auto-expired/blob/master/README.md
  2. https://social.msdn.microsoft.com/Forums/azure/en-US/72034bf9-37c9-464e-8c2f-2ca9b9424a4c/how-to-change-or-postpone-a-vm-expiration-datetime-?forum=AzureDevTestLabs
  3. https://feedback.azure.com/forums/320373-lab-services/suggestions/34793482-postpone-expiration-date-for-individual-vms-if-nee
  4. https://azure.microsoft.com/en-gb/blog/set-expiration-date-for-vms-in-azure-devtest-labs/ etc.

So, a lab user created a VM with 24 hr expiry and now wishes to keep it longer due to testing which has not concluded. Whatever we try we just get the following error back on the Set-AzureRmResource command:

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

Note our lab VM is created from an ARM template and from a lab attached shared image gallery (so a custom image in our private shared image gallery, not a public/marketplace image). As it's a private shared image gallery, it's not a true lab custom image either as such. So, in our ARM template we do not provide gallery reference or custom image ID, but more SharedImageID and SharedImageVersion.

We have tried to provide null values, or what we think may be suitable values for the "missing" properties but then we get error Set-AzureRmResource : CannotSetProperty : Cannot set or update property galleryImageReference. or Set-AzureRmResource : CannotSetProperty : Cannot set or update property customImageId.

Using AzureRm 6.13.1 but also tried Az in pwsh core and same issue.

Note that we get this error if we just get the VM object (using Get-AzureRmResource) change no properties and then set it back using Set-AzureRmResource.

Thanks!

petehauge commented 3 years ago

Hi - Good question, this is probably because of the API Version. There are some properties that have been added in a later API version. To resolve this, you can either:

  1. First get the object, modify properties and write it back, or
  2. Specify the API version

I have a handy script for updating the expiration date (prompts you for resources) that might be helpful, take a look at the attached!

Extend-DtlLabVmExpiration.zip

slime-uk commented 3 years ago

Thanks and sorry for the delay in my response. 1. I have tried to get the object, modify and set back and I get the error I described. It will not allow it as the object get does not seem complete (possibly for the API version I am using). I also tried to get, make no modifications and set back with the same error.

  1. I will have a look at your script - many thanks for providing it!