Sep 01 2021 12:32 PM - edited Jan 30 2022 08:46 PM
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.
Jan 30 2022 03:41 AM
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.
Feb 06 2022 10:29 AM
Apr 17 2022 06:03 PM - edited Apr 17 2022 06:04 PM
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
Apr 17 2022 09:53 PM
Apr 19 2022 09:04 AM
@PyungraeChoI'm doing some tests but aparently the Alert is not flagged everytime i manually shutdown the VM. Only sometimes...
Apr 19 2022 09:32 AM
Apr 25 2022 04:52 AM - edited Apr 25 2022 04:56 AM
Hi, @Galafassi
First, 5 minutes is a general result that I have tested. It is not an accurate threshold. And sometimes if the VM does not respond, it is also difficult to predict the cause. Didn't you miss tsdiscon.exe on the logoff script? If it is not, to check whether there is a problem with the VM or Azure Monitoring, additional case analysis seems to be required. Sorry...
Feb 07 2023 10:51 PM
Mar 30 2023 02:15 PM
@PyungraeCho Hi,
The process worked great until recent WVD agent updates making WVD status as 'Shutdown' rather than 'Unavailable'.
Just curious if WVDs can be ''Stopped (Deallocated)' directly from 'Shutdown' status.
Aug 16 2023 03:09 PM - edited Aug 16 2023 03:09 PM
@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
Aug 22 2023 12:54 PM
Apr 05 2024 06:40 AM
@jlou65535 Hello,
I've configured an scaling plan for my personal pool but never shutdown the virtual machines.
I had followed your link https://jlou.eu/avd-solo-start-and-stop/ but machines never shutdown.
I have an AVD personal pool wit 10 VMs. I've create a scaling plan and associated to the pool.
My scaling's plan configuration is:
Whether I disconnect the user session or close the windows avd client the VMs never shutdown and put in the deallocated state.
How can I fix it? .
Thanks in advance.
Apr 05 2024 06:49 AM
Apr 05 2024 06:56 AM - edited Apr 05 2024 07:11 AM
Yess, but doesn't work. Machines keeps alive.
Apr 05 2024 07:07 AM
Apr 05 2024 07:22 AM
Apr 05 2024 07:26 AM
Apr 05 2024 07:41 AM
Hello,
Ok, I understand now. But the only app I can see is Windows Virtual Desktop Client, with the following application id fa4345a4-a730-4230-84a8-7d9651b86739 no 9cdead84-a844-4324-93f2-b2e6bb768d07 as you recommend.
Best,
Apr 05 2024 07:49 AM