|
| 1 | +--- |
| 2 | +title: Guide to controlling Windows shutdown behavior in Azure Lab Services | Microsoft Docs |
| 3 | +description: Steps to automatically shutdown an idle Windows virtual machine and remove the Windows shutdown command. |
| 4 | +services: lab-services |
| 5 | +documentationcenter: na |
| 6 | +author: spelluru |
| 7 | +manager: |
| 8 | +editor: '' |
| 9 | + |
| 10 | +ms.service: lab-services |
| 11 | +ms.topic: article |
| 12 | +ms.date: 3/30/2020 |
| 13 | +ms.author: spelluru |
| 14 | +--- |
| 15 | + |
| 16 | +# Guide to controlling Windows shutdown behavior |
| 17 | + |
| 18 | +Azure Lab Services provides several cost controls to ensure that Windows virtual machines (VMs) are not running unexpectedly: |
| 19 | + - [Set a schedule](https://docs.microsoft.com/azure/lab-services/classroom-labs/tutorial-setup-classroom-lab#set-a-schedule-for-the-lab) |
| 20 | + - [Set quotas for users](https://docs.microsoft.com/azure/lab-services/classroom-labs/how-to-configure-student-usage#set-quotas-for-users) |
| 21 | + - [Enable automatic shutdown on disconnect](https://docs.microsoft.com/azure/lab-services/classroom-labs/how-to-enable-shutdown-disconnect) |
| 22 | + |
| 23 | +Even with these cost controls, there are situations where a Windows VM may unexpectedly continue to run; and as a result, deduct from the student's quota: |
| 24 | + |
| 25 | +- **RDP window is left open** |
| 26 | + |
| 27 | + When a student connects to their VM using RDP, they may inadvertently leave the RDP window open. As long as the RDP window remains open, the **automatic shutdown on disconnect** setting will never take effect since it is only triggered after the RDP session is disconnected. |
| 28 | + |
| 29 | +- **Windows shutdown command is used to turn off the VM** |
| 30 | + |
| 31 | + A student may use Windows shutdown command, or other shutdown mechanisms provided within Windows, to turn off the VM instead of using [Azure Lab Services' stop button](https://docs.microsoft.com/azure/lab-services/classroom-labs/how-to-use-classroom-lab#start-or-stop-the-vm). When this happens, from the perspective of Azure Lab Services, the VM is still being used. |
| 32 | + |
| 33 | +To help you prevent these situations from happening, this guide provides steps to automatically shutdown an idle Windows VM and remove the Windows shutdown command from the **Start** menu. |
| 34 | + |
| 35 | +> [!NOTE] |
| 36 | +> A VM may also unexpectedly deduct from the quota when the student starts their VM, but never actually connects to it using RDP. This guide does *not* currently address this scenario. Instead, students should be reminded to immediately connect to their VM using RDP after they start it; or, they should stop the VM. |
| 37 | +
|
| 38 | +## Automatic RDP disconnect and shutdown for idle VM |
| 39 | + |
| 40 | +Windows provides **Local Group Policy** settings that you can use to set a time limit to automatically disconnect an RDP session when it becomes idle. A session is determined to be idle when there is *not* any mouse\keyboard input. Any long running activities that don't involve mouse\keyboard input cause the VM to be in an idle state. This includes executing a long query, streaming video, compiling, etc. Depending on your class's needs, you can choose to set the idle time limit so that it's long enough to handle these types of activities. For example, you can set the idle time limit to 1 or more hours if needed. |
| 41 | + |
| 42 | +Here is the student's experience when you configure the **idle session limit** in combination with the [**automatic shutdown on disconnect**](https://docs.microsoft.com/azure/lab-services/classroom-labs/how-to-enable-shutdown-disconnect) setting: |
| 43 | + 1. The student connects to their Windows VM using RDP. |
| 44 | + 2. When the student leaves their RDP window open and the VM is idle for the **idle session limit** that you specified (such as 5 minutes), the student will see the following dialog: |
| 45 | + |
| 46 | +  |
| 47 | + |
| 48 | +1. If the student does *not* click **OK**, their RDP session will automatically disconnect after 2 minutes. |
| 49 | +2. After the RDP session disconnects, once the specified time frame for the **automatic shutdown on disconnect** setting is reached, the VM is automatically shutdown by Azure Lab Services. |
| 50 | + |
| 51 | +### Set RDP idle session time limit on the template VM |
| 52 | + |
| 53 | +To set the RDP session idle time limit, you can connect to the template VM and execute the below PowerShell script. |
| 54 | + |
| 55 | +```powershell |
| 56 | +# The MaxIdleTime is in milliseconds; by default, this script sets MaxIdleTime to 15 minutes. |
| 57 | +$maxIdleTime = 15 * 60 * 90 |
| 58 | +
|
| 59 | +Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" -Name "MaxIdleTime" -Value $maxIdleTime -Force |
| 60 | +``` |
| 61 | +Or, you can choose to follow these manual steps using the template VM: |
| 62 | + |
| 63 | +1. Press the Windows key, type **gpedit**, then select **Edit group policy (Control panel)**. |
| 64 | + |
| 65 | +1. Go to **Computer Configuration > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Session Time Limits**. |
| 66 | + |
| 67 | +  |
| 68 | + |
| 69 | +1. Right-click **Set time limit for active but idle Remote Desktop Services sessions**, and click **Edit**. |
| 70 | + |
| 71 | +1. Enter the following settings, and then click **OK**: |
| 72 | + 1. Select **Enabled**. |
| 73 | + 1. Under **Options**, specify the **Idle session limit**. |
| 74 | + |
| 75 | +  |
| 76 | + |
| 77 | +1. Finally, to combine this behavior with the **automatic shutdown on disconnect** setting, you should follow steps in the how-to article: [Enable automatic shutdown of VMs on disconnect](https://docs.microsoft.com/azure/lab-services/classroom-labs/how-to-enable-shutdown-disconnect). |
| 78 | + |
| 79 | +## Remove Windows shutdown command from Start menu |
| 80 | + |
| 81 | +Windows **Local Group Policy** settings also allow you to remove the shutdown command from the **Start** menu. |
| 82 | + |
| 83 | +To remove the shutdown command, you can connect to the template VM and execute the below PowerShell script. |
| 84 | + |
| 85 | +```powershell |
| 86 | +Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" -Name "HidePowerOptions" -Value 1 -Force |
| 87 | +``` |
| 88 | + |
| 89 | +Or, you can choose to follow these manual steps using the template VM: |
| 90 | + |
| 91 | +1. Press the Windows key, type **gpedit**, then select **Edit group policy (Control panel)**. |
| 92 | + |
| 93 | +1. Go to **Computer Configuration > Administrative Templates > Start Menu and Taskbar**. |
| 94 | + |
| 95 | +  |
| 96 | + |
| 97 | +1. Right-click **Remove and prevent access to the Shut Down, Restart, Sleep, and Hibernate commands**, and click **Edit**. |
| 98 | + |
| 99 | +1. Select the **Enabled** setting and then click **OK**: |
| 100 | + |
| 101 | +  |
| 102 | + |
| 103 | +1. Notice that the shutdown command no longer appears under Windows **Start** menu; only the **Disconnect** command appears. |
| 104 | + |
| 105 | +  |
| 106 | + |
| 107 | +## Next steps |
| 108 | +See the article on how to prepare a Windows template VM: [Guide to setting up a Windows template machine in Azure Lab Services](how-to-prepare-windows-template.md) |
0 commit comments