Automate the Generalization of an Azure VM during AVD Golden Image Preparation

When preparing a golden image for Azure Virtual Desktop (AVD), one of the critical steps is generalizing the Azure VM. Generalizing the VM ensures that the image can be used to create multiple virtual machines without retaining user-specific information or settings. This process is essential for maintaining consistency and scalability in your AVD environment.

In this blog post, we’ll walk through the steps to generalize an Azure VM and use a PowerShell script to automate the process.

GitHub Repository

I have published the complete code on my GitHub. You can access it here:
🔗 GeneralizeVM.ps1

What Does Generalizing a VM Mean?

Generalizing a VM involves removing all user-specific data, such as user accounts, machine-specific identifiers, and other custom configurations. This process prepares the VM to be used as a template (golden image) for creating multiple VMs in an AVD environment.

In Azure, a VM is generalized using the `Sysprep` tool on Windows VMs, followed by marking the VM as generalized in the Azure portal.

Steps to Generalize an Azure VM for AVD Golden Image

1. Prepare the VM

Install and configure all necessary software, applications, and updates on the VM.

Ensure the VM is in a clean state and ready to be used as a template.

2. Run Sysprep

Sysprep is a Windows tool that generalizes the operating system by removing user-specific data.

Open Command Prompt as an administrator and run the following command:

3. Mark the VM as Generalized in Azure

After Sysprep completes and the VM shuts down, go to the Azure portal.

Navigate to the VM and stop if it’s not already stopped.

Click on Capture to create an image from the VM. This marks the VM as generalized.

4. Automate the Process with PowerShell

To streamline the process, you can use the PowerShell script provided in this

🔗 GeneralizeVM.ps1

Using PowerShell Script to Generalize the VM

The script automates the generalization process, including running Sysprep and marking the VM as generalized in Azure. Here’s how to use it:

Prerequisites

Azure PowerShell module installed.

Appropriate permissions to manage Azure VMs.

The VM must be running Windows.

Steps to Use the Script

Clone the Repository

Clone the repository containing the script:

git clone https://github.com/aavisekc/avd_demo.git

Navigate to the Script

Navigate to the `ImagePrep` directory:

cd avd_demo\ImagePrep

A screenshot of the script is attached here.

A screenshot of a computer program

AI-generated content may be incorrect.

Run the Script

Run the `GeneralizeVM.ps1` script with the required parameters:

.\GeneralizeVM.ps1

Before the run please update the VM name in the parameters.json file and replace with `MyResourceGroup` and `VM` with the appropriate values.

Final Thoughts

Generalizing an Azure VM is a crucial step in preparing a golden image for Azure Virtual Desktop. By following the steps outlined in this guide and using the provided PowerShell script, you can automate and streamline the process, ensuring a consistent and scalable AVD environment.

If you have any questions or feedback, feel free to leave a comment below!

Happy imaging! 🚀

0.00 avg. rating (0% score) - 0 votes