Capture 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 disks in a storage account. The image can then be used to create multiple VMs that use managed disks for storage.

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. Click the plus sign to create a new resource.
  3. In the filter search, type Image.
  4. Select Image from the results.
  5. In the Image blade, click Create.
  6. In Name, type a name for the image.
  7. If you have more than one subscription, select the correct one from the Subscription drop-down.
  8. In Resource Group either select Create new and type in a name, or select From existing and select a resource group to use from the drop-down list.
  9. In Location, choose the location of your resource group.
  10. In OS type select the type of operating system, either Windows or Linux.
  11. In Storage blob, click Browse to look for the VHD in your Azure storage.
  12. In Account type choose Standard_LRS or Premium_LRS. Standard uses hard-disk drives and Premium uses solid-state drives. Both use locally-redundant storage.
  13. In Disk caching select the appropriate disk caching option. The options are None, Read-only and Read\write.
  14. Optional: You can also add an existing data disk to the image by clicking + Add data disk.
  15. When you are done making your selections, click Create.
  16. After the image is created, you will see it as an Image resource in the list of resources in the resource group you chose.

Create a managed 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.

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. powershell $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 a managed image of a VHD in PowerShell

Create a managed image using your generalized OS VHD.

  1. First, set the common parameters:

    $rgName = "myResourceGroupName"
    $vmName = "myVM"
    $location = "West Central US" 
    $imageName = "yourImageName"
    $osVhdUri = "https://storageaccount.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
    

Create a managed image from a snapshot using Powershell

You can also create a managed image from a snapshot of the VHD from 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
    

Next steps