Using Command Line Tools to Start and Stop Azure DevTest Lab Virtual Machines


This post explores how to extend the capabilities of Azure DevTest labs using PowerShell or the Azure CLI to start and stop lab VMs.  DevTest Labs (DTL) is a way to create fast, easy, and lean dev-test environments, with the abilities to manage cost, quickly provision VMs, and minimize waste.  Built into DTL is the feature to auto-stop and auto-start VMs within the lab at a specific time.  So why would someone want to use automation to do a task that is already built in?  It really comes down to flexibility, with a script individual machines can be started or stopped at any time not just a single, specific time without having to using the Azure Portal.  There are a few situations where this would be helpful, like using a 3-tier application as part of a test environment.  In that environment the different tiers will need to be started before the next one.  Another situation  would be as a cost saving measure where when a machine met a set of custom criteria the script would turn off the VM.  These commands could be used as a task within a CI/CD workflow to start at the beginning of the flow, use the VMs as build machines, test machines, or infrastructure, then stop the VMs when the process is complete.  An example of this would be the Custom Image Factory with Azure DevTest Labs.  Let’s start by looking at the PowerShell script to start a specific VM.

PowerShell script

<# The MIT License (MIT)
Copyright (c) Microsoft Corporation 
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.SYNOPSIS
This script starts a DevTest lab VM
.PARAMETER SubscriptionId
The subscription ID that the lab is created in.
.PARAMETER devTestLabName
The Lab Name.
.PARAMETER vMToStart
The VM Name.
.NOTES
The script assumes that the subscription exists, the DTL exists and the user has access #>
#Requires -Version 3.0
#Requires -Module AzureRM.Resources

param
(
[Parameter(Mandatory=$false, HelpMessage="The id of the subscription")]
[string] $subscriptionId = "111111-11111-11111-1111111",
[Parameter(Mandatory=$false, HelpMessage="The name of the DevTest Lab")]
[string] $devTestLabName = "yourlabname",
[Parameter(Mandatory=$false, HelpMessage="The name of the virtual machine")]
[string] $vMToStart = "vmname"  ,
[Parameter(Mandatory=$false, HelpMessage="The action on the virtual machine")]
[string] $vmAction = "Start"

)

# Select the Azure subscription
Select-AzureRMSubscription -SubscriptionId $subscriptionId

# Get the Lab / Check the AzureRm version due to changes in version 6
if ($(Get-Module -Name AzureRM).Version.Major -eq 6) {
$devTestLab = Get-AzureRmResource -ResourceType 'Microsoft.DevTestLab/labs' -Name $devTestLabName
} else {
$devTestLab = Find-AzureRmResource -ResourceType 'Microsoft.DevTestLab/labs' -ResourceNameEquals $devTestLabName
}

# Start the VM and return a succeeded or failed status
$returnStatus = Invoke-AzureRmResourceAction `
-ResourceId "$($devTestLab.ResourceId)/virtualmachines/$vMToStart" `
-Action $vmAction `
-Force

if ($returnStatus.Status -eq 'Succeeded') {
Write-Output "##[section] Successfully updated DTL machine: $vMToStart, Action: $vmAction"
}
else {
Write-Error "##[error]Failed to update DTL machine: $vMToStart, Action: $vmAction"
}

Breakdown

The parameters are straight forward, passing in the subscription Id in which the DevTest lab resides, the DevTest Lab name, and the name of the lab VM to start.  This script could be used as a function or background job within another script if multiple VMs needed to be started/stopped.

The Select-AzureRmSubscription sets the appropriate subscription, as it is possible to have identically named labs and VMs within different subscriptions.

As there are changes in the latest version of Azure PowerShell 6.0.0 the AzureRM module version determines if the Find-AzureRmResource or the Get-AzureRmResource is used.  Both get the DevTest lab object based on the resource type and the lab name.

Invoke-AzureRmResourceAction is the primary focus for this script the previous commands have been to get the proper information for the parameters.  The ResourceId parameter is the fully qualified resource id for the VM in the lab, which is generated from the DTL object resource id, the “virtualmachines” string and the VM name.  The Action parameter is where the “start” or “stop” options are set depending on what is needed.

Azure Command Line Interface

The Azure CLI is another way to automate the starting and stopping of DTL VMs, which can be installed on different operating systems.  Using the Azure CLI, you will also need the lab’s resource group name which is in the overview section of the lab portal blade .

az login – to create a connection with Azure which needs to be run before the following commands

az resource list --resource-type ”Microsoft.DevTestLab/labs” --nameyourlabname” --query “[0].resourceGroup” – get the lab Resource Group Name.

az lab vm start --lab-name yourlabname --name vmname --resource-group labResourceGroupName

az lab vm stop --lab-name yourlabname --name vmname --resource-group labResourceGroupName

 

Thoughts

The ability to manage the DevTest Lab VMs from almost anywhere allows users to use DTL VMs with minimal changes to the existing workflow and have the same or greater control.

If you have any questions about DevTest Labs, please check out the MSDN forum.
Hope this helps.

Roger Best, Senior Software Engineer

Roger is part of the Visual Studio and .NET engineering team focused on Visual Studio and Azure customers.  He has been at Microsoft for 20 years, focusing on developer technologies for the past decade or so.  In his spare time, he watches too many movies, and tries to survive triathlons


Comments (0)

Skip to main content