Create Session Hosts for Azure Virtual Desktop with PowerShell

Hello everyone!

The last post was about creating the backplane of Azure Virtual Desktop with PowerShell. If you want to read about that post click here. Now we are going to create the session hosts with just one script. This will make sure you can start with an Azure Virtual Desktop environment as Proof of Concept for your customers or for demo purposes.

Prerequisites

  • Azure subscription
  • Contributor or Owner role on the subscription (less rights are possible, but take more time to setup)
  • Powershell version 7.2.14 (newer versions have issues with this script)  Check this link to download this version: Releases · PowerShell/PowerShell (github.com)
  • Powershell modules: Az.DesktopVirtualization, Az.KeyVault, Az.Avd, Az.Compute

Quick overview of the script

This script contains multiple sections of parameters. It is possible to create one big parameter section for all parts. This is build with modules, so not every section may be needed for your setup of AVD. The script has the sections explained. I will use VSCode to run this script, but every PowerShell tool is possible. 

PowerShell script

This script has parameters that need to be changed to your needs. 

The tenant has an Id that can be found within Microsoft Entra ID in the tenant properties.

The subscription has its own Id and can be found in the Azure portal.

Make sure you run the script from my last post to have your AVD Backplane ready!

# Install modules for this script (Az.Avd is from Sander Rozemuller / https://rozemuller.com)

Install-Module Az.DesktopVirtualization
Install-Module Az.KeyVault
Install-Module Az.Avd
Install-Module Az.Compute

# Import modules for this script
Import-Module Az.DesktopVirtualization
Import-Module Az.KeyVault
Import-Module Az.Avd
Import-Module Az.Compute

# Connect to tenant with the right subscription
$parametersAz = @{
    Tenant = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    Subscription = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}


Connect-AzAccount @parametersAz

# Connect to AVD

$TenantId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$SubscriptionId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

Connect-Avd -DeviceCode -TenantID $TenantId -SubscriptionId $SubscriptionId

# Creating the keyvault for storing account information

$parametersKV = @{
    Name = 'm2cavdkeyvault01'
    ResourceGroupName = 'M2C-RG-AVD-PL-PS01'
    Location = 'westeurope'
}

$keyVault = New-AzKeyVault @parametersKV

$secretvalue = ConvertTo-SecureString "Hte2FTT4tggf" -AsPlainText -Force

$secret = Set-AzKeyVaultSecret -VaultName "m2cavdkeyvault01" -Name "domainjoinpassword" -SecretValue $secretvalue

# Creating the session hosts (I will use a marketplace image) (InitialNumber for the start of the next sessionhost number)

$HostpoolName = 'M2C-AVD-PL-PS01'
$ResourceGroupName = 'M2C-RG-AVD-PL-PS01'
$sessionHostCount = '1'
#$InitialNumber = ''
$Publisher = 'microsoftwindowsdesktop'
$Offer = 'windows-11'
$Sku = 'win11-22h2-avd'
$Location = 'westeurope'
$VmSize = 'Standard_D2s_v3'
$diskType = 'Premium_LRS'
$LocalAdmin = 'avdadmin'
$LocalPass = 'Hte2FTT4tggf2'
$Prefix = 'M2C-AVD'
$SubnetID = '/subscriptions/.../resourceGroups/M2C-RG-AVD-PL-PS01/providers/Microsoft.Network/virtualNetworks/M2C-AVD-VNET01/subnets/M2C-AVD-SUBNET01/'
$Domain = 'm2c.local'
$OU = 'OU=Pooled Desktop,OU=AVD,OU=Devices,OU=M2C,DC=m2c,DC=local'
$DomainJoinAccount = 'domainjoin@m2c.local'
$DomainJoinPassword = $secretvalue

New-AvdSessionHost -HostpoolName $HostpoolName -ResourceGroupName $ResourceGroupName -SessionHostCount $sessionHostCount -Publisher $Publisher -Offer $Offer -SKU $Sku -Location $Location -VmSize $VmSize -Disktype $Disktype -LocalAdmin $LocalAdmin -LocalPass $LocalPass -Prefix $Prefix -SubnetID $SubnetID -Domain $Domain -OU $OU -DomainJoinAccount $DomainJoinAccount -DomainJoinPassword $DomainJoinPassword

# Configure monitoring

$parametersLAW = @{
    Location = 'westeurope'
    Name = 'm2clawavd'
    ResourceGroupName = 'M2C-RG-AVD-PL-PS01'
}

New-AzOperationalInsightsWorkspace @parametersLAW

Running the script

When you run the script you will notice this takes some while, but eventually everything is created. The session host(s) will be provided in the hostpool. Don’t you worry about the session hosts. They will be created….

Some screens to show you this succeeded (after about 20 min).

Here you got your resources in Azure!

And the session host that is available to connect!

This is really cool, because now you can create the AVD environment with only two scripts completely. This will save you time to setup the Proof of Concept for AVD when you want to start out for customers.

Final Thoughts

Now the AVD environment is complete in about 30 minutes of scripting, will show you how easy it can be to set this up. These two blogs really gave me some insights about the automation with powershell. The scripts are not that long. I hope this will help you out to start creating AVD environments when you are short in time. Feel free to comment me on this post.

Author

  • Mischa Sachse

    Mischa Sachse is one of the founders of the Cloud Experts Community. Would you like to join in the fun? Make sure to contact him via the mail button below or find out more about him on his personal website.

    View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *