Azure Virtual Desktop & Printer Drivers

Paul-Christiaan Diks | 10 October 2024 | 4 min read

For several of my clients, I designed an Azure Virtual Desktop (AVD) environment tailored to the needs of various user groups within their organizations. In addition to creating the design, I also worked on building the pipelines, templates, and scripts to deploy this environment fully automatically.

One of the challenges during this process was the addition of printer drivers. While adding printer drivers might seem straightforward—essentially treating them as another application to be included in the golden image—one of my clients had a large number of exotic (label) printers with a wide variety of drivers. Unfortunately, not every printer had an installation package available for the required driver. Moreover, the driver version had to precisely match the version on the print server to ensure the correct settings were applied for accurate printing.

The print server itself contained the correct versions of the drivers needed for inclusion in the golden image. To address this challenge, I developed a solution using two PowerShell scripts.

In this article, I will demonstrate how to install printer drivers from print servers into a golden image for Azure Virtual Desktop. Using two PowerShell scripts, Backup-PrintDrivers.ps1 and Add-PrinterDrivers.ps1, we will back up the printer drivers from the print servers and then add these drivers to a golden image. This process utilizes Packer for building the golden image.

Step 1: Backing Up Printer Drivers

The first step is to back up the printer drivers from your print servers. For this, we use the script Backup-PrintDrivers.ps1, which backs up the printer drivers to an Azure Storage Account.

Script: Backup-PrintDrivers.ps1

This script connects to the print server, inventories all installed printer drivers, and copies the driver files to an Azure Storage Account.

## Backup-PrintDrivers.ps1

param (
    [string]$PrintServer,
    [string]$StorageAccountName,
    [string]$StorageAccountKey,
    [string]$ContainerName
)

# Connect to the print server and retrieve installed printers
$printers = Get-Printer -ComputerName $PrintServer

# Create a temporary directory for storing driver files
$tempDir = New-TemporaryFile
Remove-Item $tempDir
New-Item -ItemType Directory -Path $tempDir

# Backup each driver's files
foreach ($printer in $printers) {
    $driver = Get-PrinterDriver -Name $printer.DriverName -ComputerName $PrintServer
    $driverFiles = $driver | Select-Object -ExpandProperty FilePath

    foreach ($file in $driverFiles) {
        Copy-Item -Path $file -Destination $tempDir
    }
}

# Upload files to Azure Storage
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Set-AzStorageBlobContent -File $tempDir -Container $ContainerName -Context $context

# Cleanup temporary files
Remove-Item $tempDir -Recurse

Step 2: Adding Printer Drivers to the Golden Image

Once the printer drivers are backed up to the Azure Storage Account, the next step is to add these drivers to the golden image using the Add-PrinterDrivers.ps1 script. This script downloads the driver files from the storage account and installs them on the local machine (the golden image).

Script: Add-PrinterDrivers.ps1

This script retrieves the driver files from Azure Storage and installs them on the local system.

# Add-PrinterDrivers.ps1

param (
    [string]$StorageAccountName,
    [string]$StorageAccountKey,
    [string]$ContainerName
)

# Create a temporary directory for the driver files
$tempDir = New-TemporaryFile
Remove-Item $tempDir
New-Item -ItemType Directory -Path $tempDir

# Download driver files from Azure Storage
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzStorageBlob -Container $ContainerName -Context $context | ForEach-Object {
    Get-AzStorageBlobContent -Blob $_.Name -Container $ContainerName -Destination $tempDir -Context $context
}

# Install each driver
$driverFiles = Get-ChildItem -Path $tempDir

foreach ($file in $driverFiles) {
    Add-PrinterDriver -Name $file.Name -InfPath $file.FullName
}

# Cleanup
Remove-Item $tempDir -Recurse

Step 3: Building the Golden Image with Packer

Now that the scripts are ready, the final step is to integrate them into your Packer build process to create the golden image. Below is an example of a Packer template that incorporates the script for installing the drivers.

variable "storage_account_name" {
  type    = string
  default = ""
}

variable "storage_account_key" {
  type    = string
  default = ""
}

variable "container_name" {
  type    = string
  default = ""
}

source "azure-arm" "my_image" {
  os_type                 = "Windows"
  image_publisher         = "MicrosoftWindowsDesktop"
  image_offer             = "windows-10"
  image_sku               = "20h2-pro"
  managed_image_resource_group_name = "myResourceGroup"
  managed_image_name      = "myGoldenImage"
  location                = "East US"
  vm_size                 = "Standard_DS2_v2"
}

build {
  sources = ["source.azure-arm.my_image"]

  provisioner "powershell" {
    inline = [
      "powershell.exe -ExecutionPolicy Bypass -File Add-PrinterDrivers.ps1 -StorageAccountName $env:STORAGE_ACCOUNT_NAME -StorageAccountKey $env:STORAGE_ACCOUNT_KEY -ContainerName $env:CONTAINER_NAME"
    ]
  }
}

Conclusion

By following these steps, you can automate the process of installing printer drivers from one or more print servers into a golden image for Azure Virtual Desktop. This approach ensures consistency and efficiency in your AVD implementations. The included PowerShell scripts handle the backup and restoration of printer drivers, while Packer automates the image creation process.

If you have any questions or need further assistance, feel free to leave a comment below. Good luck with your implementation!