Create a managed image of a generalized VM in Azure

A managed image resource can be created from a generalized VM that is stored as either a managed disk or an unmanaged disk in a storage account. The image can then be used to create multiple VMs.

Generalize the Windows VM using Sysprep

Sysprep removes all your personal account information, among other things, and prepares the machine to be used as an image. For details about Sysprep, see How to Use Sysprep: An Introduction.

Make sure the server roles running on the machine are supported by Sysprep. For more information, see Sysprep Support for Server Roles

Important

If you are running Sysprep before uploading your VHD to Azure for the first time, make sure you have prepared your VM before running Sysprep.

  1. Sign in to the Windows virtual machine.
  2. Open the Command Prompt window as an administrator. Change the directory to %windir%\system32\sysprep, and then run sysprep.exe.
  3. In the System Preparation Tool dialog box, select Enter System Out-of-Box Experience (OOBE), and make sure that the Generalize check box is selected.
  4. In Shutdown Options, select Shutdown.
  5. Click OK.

    Start Sysprep

  6. When Sysprep completes, it shuts down the virtual machine. Do not restart the VM.

Create a managed image in the portal

  1. Open the portal.
  2. In the menu on the left, click Virtual Machines and then select the VM from the list.
  3. In the page for the VM, on the upper menu, click Capture.
  4. In Name, type the name that you would like to use for the image.
  5. In Resource group either select Create new and type in a name, or select Use existing and select a resource group to use from the drop-down list.
  6. If you want to delete the source VM after the image has been created, select Automatically delete this virtual machine after creating the image.
  7. When you are done, click Create.
  8. After the image is created, you will see it as an Image resource in the list of resources in the resource group.

Create an image of a VM using Powershell

Creating an image directly from the VM ensures that the image includes all of the disks associated with the VM, including the OS Disk and any data disks. This example shows how to create a managed image from a VM that uses managed disks.

Before you begin, make sure that you have the latest version of the AzureRM.Compute PowerShell module. Run the following command to install it.

Install-Module AzureRM.Compute -RequiredVersion 2.6.0

For more information, see Azure PowerShell Versioning.

  1. Create some variables.

    $vmName = "myVM"
    $rgName = "myResourceGroup"
    $location = "EastUS"
    $imageName = "myImage"
    
  2. Make sure the VM has been deallocated.

    Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Force
    
  3. Set the status of the virtual machine to Generalized.

    Set-AzureRmVm -ResourceGroupName $rgName -Name $vmName -Generalized
    
  4. Get the virtual machine.

    $vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName
    
  5. Create the image configuration.

    $image = New-AzureRmImageConfig -Location $location -SourceVirtualMachineId $vm.ID 
    
  6. Create the image.

    New-AzureRmImage -Image $image -ImageName $imageName -ResourceGroupName $rgName
    

    Create an image from a managed disk using PowerShell

If you only want to create an image of the OS disk, you can also crate an image by specifying the managed disk ID as the OS disk.

  1. Create some variables.

    $vmName = "myVM"
    $rgName = "myResourceGroup"
    $location = "EastUS"
    $snapshotName = "mySnapshot"
    $imageName = "myImage"
    
  2. Get the VM.

    $vm = Get-AzureRmVm -Name myVM -ResourceGroupName $rgName
    
  3. Get the ID of the managed disk.

    $diskID = $vm.StorageProfile.OsDisk.ManagedDisk.Id
    
  4. Create the image configuration.

    $imageConfig = New-AzureRmImageConfig -Location $location
    $imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -ManagedDiskId $diskID
    
  5. Create the image.

    New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
    

Create an image from a snapshot using Powershell

You can create a managed image from a snapshot of a generalized VM.

  1. Create some variables.

    $rgName = "myResourceGroup"
    $location = "EastUS"
    $snapshotName = "mySnapshot"
    $imageName = "myImage"
    
  2. Get the snapshot.

    $snapshot = Get-AzureRmSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName
    
  3. Create the image configuration.

    $imageConfig = New-AzureRmImageConfig -Location $location
    $imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -SnapshotId $snapshot.Id
    
  4. Create the image.

    New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
    

Create image from a VHD in a storage account

Create a managed image from a generalized OS VHD in a storage account. You need the URI of the VHD in the storage account, which is in the format https://mystorageaccount.blob.core.windows.net/container/vhd_filename.vhd. In this example, the VHD that we are using is in mystorageaccount in a container named vhdcontainer and the VHD filename is osdisk.vhd.

  1. First, set the common parameters:

    $vmName = "myVM"
    $rgName = "myResourceGroup"
    $location = "EastUS"
    $imageName = "myImage"
    $osVhdUri = "https://mystorageaccount.blob.core.windows.net/vhdcontainer/osdisk.vhd"
    
  2. Step\deallocate the VM.

    Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Force
    
  3. Mark the VM as generalized.

    Set-AzureRmVm -ResourceGroupName $rgName -Name $vmName -Generalized 
    
  4. Create the image using your generalized OS VHD.

    $imageConfig = New-AzureRmImageConfig -Location $location
    $imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized -BlobUri $osVhdUri
    $image = New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
    

Next steps