Azure Virtual Desktop auto shutdown and deallocate when ideal for 30 min

Abhishek Shrivastava 40 Reputation points
2024-12-29T11:53:43.4133333+00:00

Hi There

I am looking for a solution to shut down and deallocate my Virtual Machine if it is ideal for 30 min or no user is connected.

I tried scaling plan but in my case I have only one instance and just a couple of users accessing it.

I have tried auto shutdown it is working fine but need something which can keep on checking if there are active session if not it turns off the machine and deallocate.

Thanks in advance.

Azure Virtual Desktop
Azure Virtual Desktop
A Microsoft desktop and app virtualization service that runs on Azure. Previously known as Windows Virtual Desktop.
1,644 questions
{count} votes

Accepted answer
  1. Marcin Policht 31,705 Reputation points MVP
    2024-12-29T12:20:01.8166667+00:00

    Follow https://zcusa.951200.xyz/en-us/answers/questions/1690574/azure-virtual-desktop-auto-shutdown-and-power-on


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Mounika Reddy Anumandla 1,720 Reputation points Microsoft Vendor
    2024-12-30T02:00:22.07+00:00

    Hi Abhishek Shrivastava,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    As mentioned by @Marcin Policht, a similar scenario is discussed in the link. Have you had a chance to review the details provided here: https://zcusa.951200.xyz/en-us/answers/questions/1180844/how-to-automate-avd-vm-shutdown-based-on-usage-and

    The solution suggested by Priyanka in the post should help you. I am resharing it here just for your convenience.

    I am Assuming this is personal hosts that you're considering running it on, you could look at this solution: Deallocate VM on user logoff - Microsoft Community Hub. It's old but still gives you a good basis to facilitate the deallocate action. The challenge will be the determination of what's considered idle as you understand. 

    Realistically it's not going to be practical to detect the idle state to make the decision, you're best to either specifically build it to check the desired process and whether it's running or the you are better off having a host either log disconnected users off after a timeout (can be done via GPO/Intune) or have pools split to handle users who may be required to operate long running processes when disconnected (or decide which is more important, user inconvenience or better user education and cost savings). 

    Let me know if you have any further queries!

    Thank you!


  2. Abhishek Shrivastava 40 Reputation points
    2024-12-31T11:05:59.32+00:00

    I have used a different approach here to make it work.

    # Import the necessary modules

    Import-Module Az.Compute

    Import-Module Az.Accounts

    # Authenticate to Azure

    Connect-AzAccount -Identity

    # Define the resource group and VM name

    $resourceGroupName = "your-resource-group-name"

    $vmName = "your-vm-name"

    # Get the VM status

    $vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName

    $vmStatus = $vm.PowerState

    # Check if the VM is running

    if ($vmStatus -ne "VM deallocated") {

    Write-Output "The VM is not deallocated. Proceeding to stop and deallocate the VM."

    # Stop and deallocate the VM

    Stop-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -Force -AsJob

    Write-Output "The VM has been successfully deallocated."

    } else {

    Write-Output "The VM is already deallocated."

    }
    Above is a runbook script I have used it in Account automation and schedule it for one hour as that minimum available would be better I can set it up to check for 15 min ideal.

    Though this seems working for me for now.

    Many Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.