Is there a way to create a PowerShell script to stop the virtual machine automatically during the idle time of the virtual machine in azure?

Please suggest me the powershell script to stop a virtual machine IN AZURE when it attains given maximum idle time and after that it must automatically starts running when the user logs into that virtual machine?

1 answer

  • answered 2022-01-19 16:48 Ken W MSFT

    One way to accomplish this is to create a scheduled task inside the VM. That scheduled task will trigger when the machine is idle. The script the task will run is:

    Stop-AzVM -Name <VM name> -ResourceGroupName <RG Name> -Force

    As for the second part of starting when a user logs on, that is not possible. The VM will be off/de-allocated, there is nothing to log on to.

    NOTE: the VM that has the scheduled task will also need the Azure Powershell modules installed on it and a system managed identity.

    Create Task image

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum