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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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
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!
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