PowerShell How-To

Building Azure DevTest Labs with PowerShell

Microsoft's Azure DevTest Labs service allows developers and IT administrators alike to provision and control groups of virtual machines (VMs). Azure DevTest Labs allows more control over Azure resources in a test or lab environment than simply bringing up VMs.

You can set up Azure DevTest Labs in the Azure portal. Setting up labs in the portal works well for lab administrators needing to occasionally set up a lab or two. However, using the portal can soon turn into a tedious, error-prone process if you're building many labs or making lots of changes.

Using PowerShell and a community module, you can quickly and easily build and maintain Azure DevTest Labs. In this article, we're going to cover how to use a freely available PowerShell module called PSAzDevTestLabs to build Azure DevTest Labs, add VMs to them and more.

Installing PSAzDevTestLabs
Before you can install the PSAzDevTestLabs module, be sure you have the Az PowerShell module installed. You can install the Az module by running Install-Module Az. This package should include the module you need for PSAzDevTestLabs (Az.Resources). Also, be sure you are authenticated to your Azure subscription with Connect-AzAccount.

You'll need to get the PSAzDevTestLabs PowerShell module downloaded and installed, as well. In a Windows PowerShell console window, run Install-Module PSAzDevTestLabs.

Once installed, you can then import it and inspect all of the commands available to you.

PS51> Import-Module psazdevtestlabs
PS51> Get-Command -Module psazdevtestlabs

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-AzDevTestLab                                   1.0.4      psazdevtestlabs
Function        Get-AzDevTestLabArtifact                           1.0.4      psazdevtestlabs
Function        Get-AzDevTestLabArtifactSource                     1.0.4      psazdevtestlabs
Function        Get-AzDevTestLabVm                                 1.0.4      psazdevtestlabs
Function        Install-AzDevTestLabArtifact                       1.0.4      psazdevtestlabs
Function        New-AzDevTestLab                                   1.0.4      psazdevtestlabs
Function        New-AzDevTestLabUser                               1.0.4      psazdevtestlabs
Function        New-AzDevTestLabVm                                 1.0.4      psazdevtestlabs
Function        Remove-AzDevTestLab                                1.0.4      psazdevtestlabs
Function        Remove-AzDevTestLabVm                              1.0.4      psazdevtestlabs

Building a Lab
An Azure DevTest Lab includes many different components. But before you can add things like VMs and artifacts, you must fist have a lab to create them in. To create a new lab, you'll use the New-AzDevTestLab command as shown below:

$subscriptionId = (Get-AzSubscription -SubscriptionName '<your  subscription name>').Id
$rgName = '<resource  group name>'
$labName = '<lab  name>'

New-AzDevTestLab -SubscriptionId $subscriptionId -ResourceGroupName $rgName -Name $labName -VirtualNetworkName TestLabvNet

You can see I've chosen to assign some values to variables. These are values you'll need a bit later. If you don't already have a resource group created, you can create one using the New-AzResourceGroup command.

The New-AzDevTestLab command will take a few few minutes to provision the lab. Once complete, you should then see the lab in the Azure Portal.

Adding VMs
Once the lab is built, you can then start adding VMs to the lab. To do that, you have the New-AzDevTestLabVM command. This command allows you to create VMs of any size and operating system.

The New-AzDevTestLabVM command, among other parameters, needs VMImageOffer, VMImagePublisher, VMImageSku and VMSize parameter values. These values can be found using a combination of other Azure commands. Below is a code snippet that shows you how to find these values for a Windows Server 2019 core image using a Standard_DS2_v2 size in the East U.S. location:

$publisher = (Get-AzVMImagePublisher -Location 'East US').where({ $_.PublisherName -eq 'MicrosoftWindowsServer' })
$offer = (Get-AzVMImageOffer -Location 'East US' -PublisherName $publisher.PublisherName).where({ $_.Offer -eq 'WindowsServer' })
$sku = (Get-AzVMImageSku -Location 'East US' -PublisherName $publisher.PublisherName -Offer $offer.Offer).where({ $_.Skus -eq '2019-Datacenter-Core' })
$size = (Get-AzVMSize -Location 'East US').where({ $_.Name -eq 'Standard_DS2_v2' })

All of these values can change depending on your location and image to create.

Once you have all of the image and size information collected, you can then pass that to the New-AzDevTestLabVM command along with some other common attributes.

$newVmParams = @{
    Name              = 'MYTESTVM'
    SubscriptionId    = $subscriptionId
    ResourceGroupName = $rgName
    LabName           = $labName
    AdminUserName     = 'adam'
    AdminPassword     = 's3cret p@ssword'
    VMImageOffer      = $sku.Offer
    VMImagePublisher  = $sku.PublisherName
    VMImageSku        = $sku.Skus
    VmSize            = $size.Name
New-AzDevTestLabVM @newVmParams

Wait a few minutes for the VM to be provisioned and it should then show up in your lab.

[Click on image for larger view.]

Adding Lab Artifacts
The PSAzDevTestLab PowerShell module has support for adding artifacts, as well. Using an artifact, you can install software packages to your VMs. Below is an example of using the Install-AzDevTestLabArtifact command to install the Chocolatey software package manager:

$source = Get-AzDevTestLabArtifactSource -LabName $labName -ResourceGroupName $rgName
$installParams = @{
    Name              = 'windows-chocolatey'
    VmName            = 'MYTESTVM'
    LabName           = $labName
    SourceName        = $source.Name
    SubscriptionId    = $subscriptionId
    ResourceGroupName = $rgName
    Parameters        = @{
        'name'  = 'packages'
        'value' = 'vcredist140,python2'
Install-AzDevTestLabArtifact @installParams

This example is especially useful because you can use Chocolatey to install other packages. I prefer to use Chocolatey because it is simpler than installing multiple packages as Azure DevTest Labs artifacts.

Cleaning Up the Lab
When you're done, use the Remove-AzDevTestLab command to remove the lab, VMs and everything else you've created.

Remove-AzDevTestLab -Name $labName -SubscriptionId $subscriptionId -ResourceGroupName $rgName -Force -Verbose

I've now shown you how to create and manage Azure DevTest Labs, VMs and artifacts. Using the command line to manage this process with PowerShell allows you to remove all of the point-and-clicking. It also allows you to integrate this code into larger automation script to quickly provision labs at scale.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.