Introduction

When you have several Azure Virtual Machines to deploy, the customization process of each system can be time consuming. Traditionally IT Admins have been using a Gold Image to deploy a machine within their organization. In order to create a Gold Image (which is also known as a Template or a Master), it is essential to create a Virtual Machine, Install the Operating System from scratch inside this Virtual Machine, and then install all the organizational prerequisites on this VM.

For instance, a prerequisite could be to install specific software such as:

  • Adobe Reader
  • Word, Outlook, Excel, …
  • VMware View Agent
  • System Center Configuration Manager Agent
  • And so on…

Or maybe to create a folder, a custom Event Log … So finally the Virtual Machine must be “syspreped” in order to create it as an image.

As a system administrator, you would like to automate this task to save time. Thanks to the “capture” feature, which is available in Azure (in the Virtual Machine blade), you will be able to create a Gold Image, and then deploy a Virtual Machine from this template. Either you can use the Azure Portal to perform this task or you can of course use Windows PowerShell. In this guide, I will explain both methods.

Azure is not magic; you have to take some important consideration before capturing a Virtual Machine:

Download Banner
  • When you will deploy a new Virtual Machine based on the Gold Image, this VM will be created in the same Azure Region where the Gold Image was running. So if your Gold Image was running in West Europe, you won’t be able to deploy a VM in West US
  • So if you need to deploy VMs to multiple Azure regions, the only workaround is to create multiple Gold images for multiple Azure Regions
  • You cannot update the Gold image using Windows update. So, the newly created Virtual Machine will need to be updated immediately after its creation

Requirements

Let’s see the prerequisites before going deeper in this guide:

  • You must have an Azure Subscription
  • You must have at least one Resource Group or create a new one during this article
  • You must have a storage account in your Azure subscription
  • A Virtual Network with an existing subnet must exist
  • And of course you must have an existing Virtual Machine so that we can generalize it

Getting Started

In this example, I created a Windows Server 1709 Virtual Machine by clicking “Create a resource”:

generalized-vm-in-azure

and then create a folder called “GET-CMD”. At this step, you can install software; copy/paste some custom scripts, or whatever you want …

generalized-vm-in-azure

Before creating an image, it is important to use “Sysprep” to prepare the Windows guest OS on the Virtual Machine. If you create an image from a Virtual Machine that has not been generalized, any Virtual Machine created from that image won’t start.

What is “Sysprep”?

“Sysprep” (System Preparation) prepares a Windows installation (Windows client and Windows Server) for imaging, allowing you to capture a customized installation. “Sysprep” removes PC-specific information from a Windows installation, “generalizing” the installation so it can be installed on different PCs.

Note: Capturing a Virtual Machine image will make the Virtual Machine unusable. This action cannot be undone.

Once you have finished the customization, navigate to “C:\windows\system32\sysprep” and run “sysprep.exe”. Run the system preparation with the following settings:

Sysprep is generalizing the Virtual Machine, so wait a moment …

generalized-vm-in-azure

Note: Sometimes, you may get an error during the sysprep process, which is very common. To solve this issue, you have to navigate to the following directory:

C:\windows\system32\sysprep\panther\

Open the log file and look at near the bottom of the log file, and you will see that there are some error messages that mention a certain application or something else. You have to correct this error, and try to sysprep again the machine.

generalized-vm-in-azure

Once the generalization is done, the Virtual Machine will stop. Open the Azure Portal, and confirm the status:

generalized-vm-in-azure

Click on your Virtual Machine, and then notice the “Capture” button. Azure allows you to convert a Standard Virtual Machine to a Template (e.g Gold Image).

generalized-vm-in-azure

A new wizard will appear and you will be prompted to type a name, and to select a Resource Group. As I mentioned before, capturing a Virtual Machine image will make the Virtual Machine unusable, so you can check the box to delete the Virtual Machine after creating the image.

generalized-vm-in-azure

Click OK, and wait until the Virtual Machine is converted. Once the template is created, navigate to “All services” and “Images”:

generalized-vm-in-azure

And you will notice the Gold Image.

generalized-vm-in-azure

From this image, you can click “+ Create VM” to create a new Virtual Machine based on this template.

generalized-vm-in-azure

In the new blade that appears, you must type a name for the newly Virtual Machine:

generalized-vm-in-azure

Wait until the deployment is done. In my case, the deployment took only 4 minutes 10 seconds. Now let’s connect to this Virtual Machine and confirm if the folder exists or not.

generalized-vm-in-azure

The folder exists and the last write time is the same as the “SysprepVM”, so everything is ok, the “VM01” is up and running.

Windows PowerShell

Automating IT admin tasks is essential for every sysadmin, so you should use Windows PowerShell to generalize a Virtual Machine. Thanks to Windows PowerShell and the Azure RM PowerShell Module, you will be able to automate the whole process in Azure.

Firstly, as we did in the Azure Portal, we need to stop the Virtual Machine:

Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Force

generalized-vm-in-azure

Here, we need to use the Set-AzureRmVm cmdlet to generalize the VM:

Set-AzureRmVm -ResourceGroupName $rgName -Name $vmName -Generalized

generalized-vm-in-azure

The last step retrieves the Virtual Machine information, then we create a new Azure Image Configuration in the West Europe region, and finally, we can create the Azure Image using the New-AzureRmImage cmdlet:

$vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName
$image = New-AzureRmImageConfig -Location $location -SourceVirtualMachineId $vm.ID
New-AzureRmImage -Image $image -ImageName $imageName -ResourceGroupName $rgName

generalized-vm-in-azure

Once the template is created, navigate to “All services” and “Images” to confirm the newly Azure Image:

generalized-vm-in-azure

Conclusion

In this article, we discussed about Azure Images, which are very important for Azure Admins in order to deploy multiple Virtual Machines in Azure based on a template. We also discussed about Windows PowerShell to automate this task and to save time. You can use the Azure Portal instead of PowerShell if you are not very familiar with the command line.

Thanks to Azure Images, you can create Azure Virtual Machines based on your own template which is essential to suit your needs.

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

Like what you read? Rate us
How to create an Image of a Generalized VM in Azure
Rate this post