Create custom session host images by using image templates
This lab guide outlines the process of creating custom session host images for Azure Virtual Desktop using image templates. Here's a breakdown of the key steps involved:
Instructions
Register Required Resource Providers
Start a PowerShell session in the Azure Cloud Shell.
Run the following command to register the necessary resource providers:
PowerShell
Register-AzResourceProvider -ProviderNamespace Microsoft.DesktopVirtualization
Register-AzResourceProvider -ProviderNamespace Microsoft.VirtualMachineImages
Register-AzResourceProvider -ProviderNamespace Microsoft.Storage
Register-AzResourceProvider -ProviderNamespace Microsoft.Compute
Register-AzResourceProvider -ProviderNamespace Microsoft.Network
Register-AzResourceProvider -ProviderNamespace Microsoft.KeyVault
Register-AzResourceProvider -ProviderNamespace Microsoft.ContainerInstance
Task 2: Create a User-Assigned Managed Identity
In the Azure portal, search for and select "Managed Identities" and Click "+ Create."
On the "Basics" tab, configure the following and then click on create:
Subscription: Your Azure subscription
Resource group: Create a new resource group:
az140-15a-RGName:
az140-<random>-uami
Task 3: Create a Custom RBAC Role
Open PowerShell in Azure Cloud Shell.
Run the following command to get your subscription ID:
$subscriptionId = (Get-AzSubscription).Id
- Run the following command, to define the custom role:
$jsonContent = @"{
"Name": "Desktop Virtualization Image Creator (<random>)",
"IsCustom": true,
"Description": "Create custom image templates for Azure Virtual Desktop images.",
"Actions": [
"Microsoft.Compute/galleries/read",
"Microsoft.Compute/galleries/images/read",
"Microsoft.Compute/galleries/images/versions/read",
"Microsoft.Compute/galleries/images/versions/write",
"Microsoft.Compute/images/write",
"Microsoft.Compute/images/read",
"Microsoft.Compute/images/delete"
],
"NotActions": [],
"DataActions": [],
"NotDataActions": [],
"AssignableScopes": [
"/subscriptions/$subscriptionId",
"/subscriptions/$subscriptionId/resourceGroups/az140-15b-RG"
]
}"@
- Save the role definition to a file:
$jsonContent | Out-File -FilePath 'CustomRole.json'
- Create the custom role:
New-AzRoleDefinition -InputFile ./CustomRole.json
- Close the Cloud Shell.
Task 4: Set Permissions on Host Image Provisioning Resources
Create a new resource group:
az140-15b-RGand select "Access control (IAM)." on the created resource group.Click "+ Add" and then "Add role assignment."
On the "Role" tab, search for and select your custom role: "Desktop Virtualization Image Creator “
On the "Members" tab, select "Managed identity," click "+ Select members," choose "User-assigned managed identity," select
az140-<random>-uami, and click "Select."Click "Review + assign" and then "Review + assign."
Task 5: Create Azure Compute Gallery and Image Definition
Search for and select "Azure compute galleries." and Click "+ Create."
On the "Basics" tab:
Resource group:
az140-15b-RGName:
az14015computegallery
On the "Sharing" tab, leave the default "Role based access control (RBAC)" selected.
Click "Review + create" and then "Create."
Navigate to the
az14015computegalleryinstance.Click "+ Add" and then "+ VM image definition."
On the "Basics" tab:
Region: Your preferred region
VM image definition name:
az14015imagedefinitionOS type: Windows
Security type: Trusted launch supported
OS state: Generalized
Publisher: MicrosoftWindowsDesktop
Offer: Windows-11
SKU: win11-23h2-avd-m365
Click "Create."
Task 6: Create a Custom Image Template
Search for and select "Azure Virtual Desktop."
In the "Manage" section, select "Custom image templates."
Click "+ Add custom image template."
On the "Basics" tab:
Template name:
az140-15b-imagetemplateImport from existing template: No
Resource group:
az140-15b-RGManaged identity:
az140-<random>-uami
On the "Source image" tab:
Source type: Platform image (marketplace)
Select image: Windows 11 Enterprise multi-session, Version 23H2 + Microsoft 365 Apps
On the "Distribution targets" tab:
Azure Compute Gallery: Enabled
Gallery name:
az14015computegalleryGallery image definition:
az14015imagedefinitionGallery image version:
1.0.0Run output name:
az140-15-image-1.0.0Replication regions: Your preferred region
Exclude from latest: No
Storage account type: Standard_LRS
Task 7 :Create a custom image template
Navigate to the "Create custom image template" page in the Azure portal.
On the "Build properties" tab, configure the following settings:
Build timeout: 120
Build VM size: Standard_DC2s_v3
OS disk size (GB): 127
Staging group:
az140-15c-RG(This resource group will store build resources and logs. If not specified, it will be auto-generated.)VNet: Leave unset (A temporary VNet and public IP will be created for the build VM.)
Important: Ensure you have sufficient vCPU quota for the chosen Build VM size. If not, select a different size or request a quota increase.
Click "Next."
On the "Customization" tab, click "+ Add built-in script."
In the "Select built-in scripts" pane, choose the following scripts:
Time zone redirection
Disable Storage Sense
Enable screen capture protection (with "Block Screen capture on client and server" selected)
Click "Save." (You can add custom scripts later, referencing the built-in scripts for examples.)
Click "Next."
On the "Tags" tab, click "Next."
On the "Review + create" tab, click "Create." (Template creation may take a few minutes. Monitor the status on the "Azure Virtual Desktop | Custom image templates" page.)
Task 8: Build a custom image (Optional - time intensive)
On the "Azure Virtual Desktop | Custom image templates" page, select
az140-15b-imagetemplate.Click "Start build." (The build process can take up to 45 minutes. Monitor the "Build run state" on the template page, refreshing periodically.)
- The state will progress from "Running - Building" to "Running - Distributing" and finally "Succeeded."
(Optional) While waiting, explore the
az140-15c-RGresource group. This group contains the auto-provisioned build resources: build VM, VNet, network security group, key vault, snapshot, container instance, and storage account. You can view these by searching for "Resource groups" and selectingaz140-15c-RG.Monitor the build progress on the
az140-15b-imagetemplatepage or use the Activity Log (search for "Execute a VM image template to produce its output" and check its status).Once the build completes, navigate to "Azure compute galleries" and select
az14015computegallery.On the "Definitions" tab, select
az14015imagedefinition.On the "Versions" tab, review the
1.0.0(latest version) image information.
Task 9: Deploy session hosts using the custom image (Optional)
Search for and select "Virtual networks," then click "Create +."
On the "Basics" tab, configure:
Subscription: Your Azure subscription
Resource group: Create a new resource group named
az140-15d-RGVirtual network name:
az140-vnet15dRegion: eastus2
Click "Next" through the "Security" tab.
On the "IP addresses" tab:
IP address space:
10.30.0.0/16Edit the "default" subnet:
Name:
hp1-SubnetStarting address:
10.30.1.0Enable private subnet (no default outbound access): Disabled
Click "Save," then "Review + create," and finally "Create."
Search for and select "Azure Virtual Desktop." Navigate to "Host pools" and click "+ Create."
On the "Basics" tab, configure:
Subscription: Your Azure subscription
Resource group:
az140-15d-RGHost pool name:
az140-15-hp1Location: eastus2
Validation environment: No
Preferred app group type: Desktop
Host pool type: Pooled
Create Session Host Configuration: No
Load balancing algorithm: Breadth-first
On the "Session hosts" tab:
Add virtual machines: Yes
Resource group: (Defaulted to the host pool's resource group)
Name prefix: (Important) Find the string between "User1-" and "@" in your lab session window's Resources tab and use it here. Example:
sh048577130Virtual machine type: Azure virtual machine
Virtual machine location: eastus2
Availability options: No infrastructure redundancy required
Security type: Trusted launch virtual machines
On the "Virtual machines" tab:
Image: Click "See all images," select "Shared images," and choose
az14015imagedefinition.Virtual machine size: Standard_DC2s_v3
Number of VMs: 1
OS disk type: Standard SSD
OS disk size: Default size (128 GiB)
Boot Diagnostics: Enable with managed storage account (recommended)
Virtual network:
az140-vnet15dSubnet:
hp1-SubnetNetwork security group: Basic
Public inbound ports: No
Select which directory you would like to join: Microsoft Entra ID
Enroll VM with Intune: No
User name: Student
Password: A strong password (at least 12 characters, mixed case, numbers, and special characters).
Confirm password: (Re-enter the password)
On the "Workspace" tab, confirm "Register desktop app group" is set to "No" and click "Review + create."
On the "Review + create" tab, click "Create." (Deployment may take 10-15 minutes.)
Remember, this is a concise summary of the lab instructions. For detailed steps and specific settings, please refer to the complete lab manual here.