We love to hear about all the types of classes being taught using Azure Lab Services. Recently, a school informed us they were teaching an introduction to networking class using nested virtualization. While they loved the contained nature of using Azure Lab Services, students were occasionally changing the IP address of their lab VM instead of the Hyper-V VM. Once the changes have been saved the students can no longer connect to the VM and any work is lost. While there isn't a way to fix existing issues, there is a way to help prevent those issues by setting up a shutdown script. The script in this article will only change the IP address and DNS address to dynamic, so if the student sets a static ip it will be reverted. If there are other areas where students may accidentally make changes then modify the PowerShell script to handle those specific changes.
There are two parts to this, the PowerShell script and setting up the Local Group Policy to run the script at shutdown. First create a PowerShell script on the template VM. In this example the code would be:
This script will reset the ip address and dns server ip to dynamic..
The second part is to configure the operating system to execute the script on shut down. This is done using the Local Group Policy.
Open the Local Group Policy editor (gpedit.msc)
Under Local Computer Policy / Computer Configuration / Windows Settings / Scripts (Startup/Shutdown)
Open the Shutdown property.
Select PowerShell scripts from the top tabs.
Add the location and file for the PowerShell script above.
Change the run order (below the script) to be “Run Windows PowerShell scripts first”
Publish the template and if any student changes the host virtual machine ip address it will be changed back to dynamic on shutdown. This script will be executed on O.S. shutdown and any of the lab settings (O.S. idle, disconnect, not connected).