As of today's writing, the Microsoft Azure PowerShell package of modules version 2.3.2 still has yet to have any real commands to work with Azure DevTest Labs. Pity. However, it's still possible using some ingenuity and a little PowerShell to build DevTest labs, add VMs, add artifacts and more. We just need to build it.

Lucky for you, I've put together a small PowerShell module called PsAzDevTestLabs that allows you to build labs, deploy virtual machines, install artifacts on those VMs and remove everything when you're done. It's on GitHub too and could be greatly expanded upon.

Install the module from the PowerShell Gallery by running Install-Module -Name PSAzDevTestLabs and be sure you're connected to your Azure subscription by running Connect-AzAccount. Once that's done, you're ready to begin.

Building the Azure DevTest Lab

Creating the Azure DevTest lab is a simple one-liner but we first need to get our subscription ID. I'm assuming here you only have one subscription. If you have multiple subscription, be sure to use the SubscriptionName parameter.

PS> $subscriptionId = (Get-AzSubscription).Id

Next, we can create the lab using the subscription ID, a resource group of our choosing and a virtual network name the lab will use.

PS> New-AzDevTestLab -SubscriptionId $subscriptionId -ResourceGroupName 'LabRg' -Name 'MyLab' -VirtualNetworkName TestLabvNet

This will take a bit to run.

Building Lab Virtual Machines (VMs)

Once the lab has been built, we can then start to deploy VMs to it. To do that, we have the New-AzDevTestLabVm command. This function, as is, is bare-bones. Currently, it's only capable of building Windows VMs. A ton of functionality can be added on here.

First, we need to find a few things such as the VM image offer, publisher and SKU. Unfortunately, there's no quick way to grab these attributes so we'll need to call a few Azure PowerShell cmdlets to grab them as shown below.

You'll see that I'm pulling resources from the East US region which is closest to me. I'm getting the SKU for Windows Server 2019 Datacenter Core as well as picking the Standard_DS2_V2 size for the VM I will be creating.

$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' })

Once I have the SKU object, I can pull the offer, publisher and SKU needed from it. We'll also need to define the name of the VM, the lab name, administrator username and password and finally the size of the VM.

$newVmParams = @{
    Name              = 'MyFirstLabVM'
    SubscriptionId    = $subscriptionId
    ResourceGroupName = 'LabRg'
    LabName           = 'MyLab'
    AdminUserName     = 'adam'
    AdminPassword     = 'sup3rSecret!'
    VMImageOffer      = $sku.Offer
    VMImagePublisher  = $sku.PublisherName
    VMImageSku        = $sku.Skus
    VmSize            = $size.Name
}
New-AzDevTestLabVM @newVmParams

Once the above command is kicked off, it will begin building the VM inside of the MyLab lab.

Installing Artifacts on VMs

Next, we can add some artifacts to the VMs as well. DevTest Labs has a nice system that allows you to install packages known as artifacts to the lab VMs. But, I like to use Chocolatey for this sort of thing. Using the Install-AzDevTestLabArtifact, we'll just deploy the window-chocolatey package to bootstrap that process.

Once that's installed, I'll use it to install the vcredist140 and python2 Chocolatey packages as examples.

Note that to find the Chocolatey package names, you could go the the Chocolatey website or if you have the Chocolatey PowerShell module installed locally you can run Get-ChocolateyPackage or choco list to find the package(s) you want to install. You will provide these as the value value in the Parameters parameter.

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

Azure DevTest Labs also allows you to build your own artifacts and deploy them. You could either do that or decide to package them up with Chocolatey. You've got a couple different options there.

Adding Users to the Lab

Perhaps I want to add a user to the lab. In that case, it's a simple one-liner using the New-AzDevTestLabUser command.

PS> New-AzDevTestLabUser -DisplayName 'Adam Bertram' -LabName 'MyLab' -ResourceGroupName 'LabRg' -SubscriptionId $subscriptionId

Removing VMs and the Lab

Finally, we're done with the lab and all of the VMs we've created and it's time to destroy everything we just did. For that, it's another one-liner using Remove-AzDevTestLab.

PS> Remove-AzDevTestLab -Name 'MyLab' -SubscriptionId $subscriptionId -ResourceGroupName 'LabRG' -Force -Verbose

Summary

I wish I didn't have to build this Azure DevTest Labs module but I'm glad I could. Using this module will provide you with a great start to completely automating most of your Azure DevTest Labs resource deployments in PowerShell.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!