Automate the Creation of Azure Virtual Desktop Host and Host Pools – Create and Configure Host Pools and Session Hosts

Automate the Creation of Azure Virtual Desktop Host and Host Pools

The following sections cover creation of azure virtual desktop including session host, hostpool and hostpool config using Powershell, Azure CLI, ARM. If you are thinking about automating Azure Virtual Desktop deployment then this section will help you to understand all automation options.

Personal Host Pool Creation Using PowerShell/CLI or ARM

In this section, you will see the automation options for creating a personal host pool. The PowerShell module Az.DesktopVirtualization allows you to create a personal host pool as well as add a session host in the personal host pool.

I always recommend using the Azure Resource Manager (ARM) template wherever it is possible to get additional benefits from IAC.

Let’s get started with the steps for PowerShell and ARM. The following are the resources that need to be created as part of an Azure Virtual Desktop host pool:

•\   Resource group to group all host pool–related resources

•\   AVD workspace to group multiple host pools

•\   Personal host pool and app group

•\   VM/session host creation

•\   Domain join extension

•\   Azure ARM JsonADDomainExtension extension to join a VM to the host pool with a host pool token

The full PowerShell and ARM code to create an Azure Virtual Desktop personal desktop can be found at https://github.com/Ar-Sa/Arun/blob/master/PowerShell/ PowerShell%20and%20ARM%20to%20create%20Azure%20virtual%20desktop%20 personal%20desktop/New-AVDPersonalHostpool.ps1.

First you have to log in to Azure using the Connect-AzAccount command and select the correct subscription in which you want to create the Azure Virtual Desktop host pool. You can create a new resource group and the AVD workspace using the following PowerShell command, or if you already have a resource group and workspace, then you can skip this step. The following command also checks if the resource group and workspace are already present in the selected subscription, and if it is, then PowerShell will skip the resource creation. See Figure 8-15.

Figure 8-15.  Azure Virtual Desktop workspace creation via PowerShell

The cmdlet New-AzWvdHostPool will create the host pool, and you can use the New-AzWvdApplicationGroup cmdlet to create a desktop application group. Additionally, Register-AzWvdApplicationGroup will register the desktop app group with the workspace. You can either create a workspace with this cmdlet or use an existing workspace.

The cmdlet New-AzWvdRegistrationInfo creates a registration token to authorize a session host to join the host pool and save it to the $token variable. You can specify how long the registration token is valid by using the -ExpirationTime parameter. The token’s expiration date can be no less than an hour and no more than one month. If you set -ExpirationTime outside of that limit, the cmdlet won’t create the token. Note that ExpirationTime accepts the date and time in a specific format, so you have to convert the value to a string in the yyyy-MM-ddTHH:mm:ss.fffffffZ format. See Figure 8-16.

Figure 8-16.  Azure Virtual Desktop host pool creation via PowerShell

The next step is to create the session host and register it with the host pool with the correct token we generated earlier. The New-AzResourceDeployment cmdlet will trigger the ARM template from the URI https://raw.githubusercontent.com/Ar-Sa/Arun/ master/PowerShell/PowerShell%20and%20ARM%20to%20create%20Azure%20virtual%20 desktop%20personal%20desktop/New-personalAVDTemplate.json, and the ARM template will take care of creating the session host, domain joining, and registering session host with a host pool. You can add customization in the ARM template to make the VM/session host compliant as per your requirements. See Figure 8-17.

Figure 8-17.  Azure Virtual Desktop creation via PowerShell and ARM