Forum Discussion
Auto Deallocation for Unused Azure Virtual Desktop Personal VMs
Auto-Deallocation method for Azure Virtual Desktop VM that has not been used for a specific period has been introduced in various places. I would like to introduce another scenario. But this is suitable for use in a Personal Pool environment only. Please note that it is difficult to use in a pooled VM environment that allows multi-session.
Conditions and Methods of VM Deallocation
When the Windows Shutdown state (VM Stopped) is detected by Azure Monitor, the VM is deallocated by Azure Automation. What is important here will be the conditions and method of shutting down Windows. I implemented Shutdown action with Disconnect Trigger and Delay Task using task scheduler. I also needed another task scheduler to reset this triggered Shutdown task when the user reconnects to the VM before shutdown. The scenario below is an example, and the ‘idle time’ and ‘delay task time’ can be changed as desired by referring to the configuration snapshot.
A. When user Disconnect:
- Scheduler1 is started because 'Disconnect' status is triggered.
- After 30 minutes, Windows Shutdown (VM Stopped)
- 'VM Stopped' status detected and VM deallocated within 5 min.
B. When the user log off:
- Logoff after running tsdiscon.exe by logoff script
- Scheduler1 is started because 'Disconnect' status is triggered.
- After 30 minutes, Windows Shutdown (VM Stopped)
- 'VM Stopped' status detected and VM deallocated within 5 min.
C. When the user shuts down Windows:
- 'VM Stopped' status detected and VM deallocated within 5 min.
D. When Idle status occurs for more than 15 minutes after connection:
- Session is disconnected by AD policy related disconnection for idle session
- Scheduler1 is started because 'Disconnect' status is triggered.
- After 30 minutes, Windows Shutdown (VM Stopped)
- 'VM Stopped' status detected and VM deallocated within 5 min.
E. When a user reconnects before 30 minutes after 'Disconnect' occurs:
- Scheduler2 starts because 'Connect' status is triggered
- Scheduler2 resets (disable & enable) Scheduler1. Scheduler1 ends the task to be performed and starts trigger detection again.
F. When the user restarts Windows
- Only Windows restart is performed normally. (This is the action the user wants)
If 'shutdown' or 'deallocation' command is registered in the logoff script of personal VM, the user could not connect to the VM after restarting Windows. This is because restart involves the logoff phase. This issue occurred when installing software that required a restart or manual Windows update task .
Now let's share a screenshot for the configuration.
1. Azure Automation Account
- Create an Azure Automation Account and import the module.
2. Azure Monitor
- Monitor → Service Health → Resource Health
- The most important thing in setting an alert rule is the condition. The resource health event matching 'Unavailable' shows the Windows shutdown (VM Stopped) status or unavailable for an unexpected reason.
- Add Action group, connect with the Automation Account created earlier, and use the built-in 'Stop VM' runbook.
3. Scheduler 1 (Shutdown after 30 minutes when User ‘Disconnect’ is triggered)
- Set as below in AD GPO or client scheduler.
4. Scheduler 2 (Disable & Enable Scheduler 1 when User ‘Connect’ is triggered)
- Set as below in AD GPO or client scheduler
5. Logoff Script
- Set as below in AD or Local GPO. tsdiscon.exe is located under the system32 folder. Just copy this file to your GPO folder. This logoff script creates a Disconnet state just before logoff so that it triggers on Scheduler 1.
6. Disconnect for Idle Session
- If the idle state is maintained for a specific time, the AD policy is set so that the session is automatically disconnected.
I hope this method will help many people, and I would appreciate any feedback.
22 Replies
- ShahzaanCopper Contributor
PyungraeCho Hi,
The process worked great until recent WVD agent updates making WVD status as 'Shutdown' rather than 'Unavailable'.
https://learn.microsoft.com/en-us/answers/questions/1193772/what-is-the-different-between-shutdown-unavailable
Just curious if WVDs can be ''Stopped (Deallocated)' directly from 'Shutdown' status.
- GalafassiCopper Contributor
PyungraeCho I received an email about "Migrate your runbooks to managed identities in Azure Automation by 30 September 2023"
This is something related to this scripts? Something to do?
Regards,
Wilson
- Yes, you have to take care.
Because my env is personal AVD VMs, I decided to change it by the new feature for AVD Auto-scaling : https://jlou.eu/avd-solo-start-and-stop/
- Just tested : working well !
- GalafassiCopper Contributor
Hi, PyungraeCho
This can be used with VMS running windows server? There's some cost on this procedure like creating an Azure Automation Account?
Thank you,
Wilson- PyungraeCho
Microsoft
Hi, Galafassi
Yes. It doesn't matter if it's a Windows client or a server. As you know it will cost a bit like Azure Automation.- GalafassiCopper Contributor
PyungraeChoI'm doing some tests but aparently the Alert is not flagged everytime i manually shutdown the VM. Only sometimes...
- MarcinUKCopper Contributor
Hi PyungraeCho
This is a great post.
I was only looking for deallocation after a VM is shut down by a user and I found exactly what I needed.
All worked perfectly.
Thank you.
- PyungraeCho
Microsoft
Thanks for your reply.