Create a Windows VM from a specialized disk

Create a new VM by attaching a specialized managed disk as the OS disk using Powershell. A specialized disk is a copy of virtual hard disk (VHD) from an existing VM that maintains the user accounts, applications, and other state data from your original VM.

When you use a specialized VHD to create a new VM, the new VM retains the computer name of the original VM. Other computer-specific information is also be kept and, in some cases, this duplicate information could cause issues. Be aware of what types of computer-specific information your applications rely on when copying a VM.

You have two options:

This topic shows you how to use managed disks. If you have a legacy deployment that requires using a storage account, see Create a VM from a specialized VHD in a storage account

Before you begin

If you use PowerShell, make sure that you have the latest version of the AzureRM.Compute PowerShell module.

Install-Module AzureRM.Compute -RequiredVersion 2.6.0

For more information, see Azure PowerShell Versioning.

Option 1: Upload a specialized VHD

You can upload the VHD from a specialized VM created with an on-premises virtualization tool, like Hyper-V, or a VM exported from another cloud.

Prepare the VM

If you intend to use the VHD as-is to create a new VM, ensure the following steps are completed.

  • Prepare a Windows VHD to upload to Azure. Do not generalize the VM using Sysprep.
  • Remove any guest virtualization tools and agents that are installed on the VM (like VMware tools).
  • Ensure the VM is configured to pull its IP address and DNS settings via DHCP. This ensures that the server obtains an IP address within the VNet when it starts up.

Get the storage account

You need a storage account in Azure to store the uploaded VHD. You can either use an existing storage account or create a new one.

To show the available storage accounts, type:

Get-AzureRmStorageAccount

If you want to use an existing storage account, proceed to the Upload the VHD section.

If you need to create a storage account, follow these steps:

  1. You need the name of the resource group where the storage account should be created. To find out all the resource groups that are in your subscription, type:

    Get-AzureRmResourceGroup
    

    To create a resource group named myResourceGroup in the West US region, type:

    New-AzureRmResourceGroup -Name myResourceGroup -Location "West US"
    
  2. Create a storage account named mystorageaccount in this resource group by using the New-AzureRmStorageAccount cmdlet:

    New-AzureRmStorageAccount -ResourceGroupName myResourceGroup -Name mystorageaccount -Location "West US" `
        -SkuName "Standard_LRS" -Kind "Storage"
    

Upload the VHD to your storage account

Use the Add-AzureRmVhd cmdlet to upload the VHD to a container in your storage account. This example uploads the file myVHD.vhd from "C:\Users\Public\Documents\Virtual hard disks\" to a storage account named mystorageaccount in the myResourceGroup resource group. The file is stored in the container named mycontainer and the new file name will be myUploadedVHD.vhd.

$resourceGroupName = "myResourceGroup"
$urlOfUploadedVhd = "https://mystorageaccount.blob.core.windows.net/mycontainer/myUploadedVHD.vhd"
Add-AzureRmVhd -ResourceGroupName $resourceGroupName -Destination $urlOfUploadedVhd `
    -LocalFilePath "C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd"

If successful, you get a response that looks similar to this:

MD5 hash is being calculated for the file C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd.
MD5 hash calculation is completed.
Elapsed time for the operation: 00:03:35
Creating new page blob of size 53687091712...
Elapsed time for upload: 01:12:49

LocalFilePath           DestinationUri
-------------           --------------
C:\Users\Public\Doc...  https://mystorageaccount.blob.core.windows.net/mycontainer/myUploadedVHD.vhd

Depending on your network connection and the size of your VHD file, this command may take a while to complete

Create a managed disk from the VHD

Create a managed disk from the specialized VHD in your storage account using New-AzureRMDisk. This example uses myOSDisk1 for the disk name, puts the disk in StandardLRS storage, and uses https://storageaccount.blob.core.windows.net/vhdcontainer/osdisk.vhd as the URI for the source VHD.

Create a new resource group for the new VM.

$destinationResourceGroup = 'myDestinationResourceGroup'
New-AzureRmResourceGroup -Location $location -Name $destinationResourceGroup

Create the new OS disk from the uploaded VHD.

$sourceUri = (https://storageaccount.blob.core.windows.net/vhdcontainer/osdisk.vhd)
$osDiskName = 'myOsDisk'
$osDisk = New-AzureRmDisk -DiskName $osDiskName -Disk `
    (New-AzureRmDiskConfig -AccountType StandardLRS  -Location $location -CreateOption Import `
    -SourceUri $sourceUri) `
    -ResourceGroupName $destinationResourceGroup

Option 2: Copy an existing Azure VM

You can create a copy of a VM that uses managed disks by taking a snapshot of the VM, then using that snapshot to create a new managed disk and a new VM.

Take a snapshot of the OS disk

You can take a snapshot of and entire VM (including all disks) or of just a single disk. The following steps show you how to take a snapshot of just the OS disk of your VM using the New-AzureRmSnapshot cmdlet.

Set some parameters.

$resourceGroupName = 'myResourceGroup' 
$vmName = 'myVM'
$location = 'westus' 
$snapshotName = 'mySnapshot'  

Get the VM object.

$vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName

Get the OS disk name.

$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $vm.StorageProfile.OsDisk.Name

Create the snapshot configuration.

$snapshotConfig =  New-AzureRmSnapshotConfig -SourceUri $disk.Id -OsType Windows -CreateOption Copy -Location $location 

Take the snapshot.

$snapShot = New-AzureRmSnapshot -Snapshot $snapshotConfig -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName

If you plan to use the snapshot to create a VM that needs to be high performing, use the parameter -AccountType Premium_LRS with the New-AzureRmSnapshot command. The parameter creates the snapshot so that it's stored as a Premium Managed Disk. Premium Managed Disks are more expensive than Standard. So be sure you really need Premium before using the parameter.

Create a new disk from the snapshot

Create a managed disk from the snapshot using New-AzureRMDisk. This example uses myOSDisk for the disk name.

Create a new resource group for the new VM.

$destinationResourceGroup = 'myDestinationResourceGroup'
New-AzureRmResourceGroup -Location $location -Name $destinationResourceGroup

Set the OS disk name.

$osDiskName = 'myOsDisk'

Create the managed disk.

$osDisk = New-AzureRmDisk -DiskName $osDiskName -Disk `
    (New-AzureRmDiskConfig  -Location $location -CreateOption Copy `
    -SourceResourceId $snapshot.Id) `
    -ResourceGroupName $destinationResourceGroup

Create the new VM

Create networking and other VM resources to be used by the new VM.

Create the subNet and vNet

Create the vNet and subNet of the virtual network.

Create the subNet. This example creates a subnet named mySubNet, in the resource group myDestinationResourceGroup, and sets the subnet address prefix to 10.0.0.0/24.

$subnetName = 'mySubNet'
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24

Create the vNet. This example sets the virtual network name to be myVnetName, the location to West US, and the address prefix for the virtual network to 10.0.0.0/16.

$vnetName = "myVnetName"
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $destinationResourceGroup -Location $location `
    -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet

Create the network security group and an RDP rule

To be able to log in to your VM using RDP, you need to have a security rule that allows RDP access on port 3389. Because the VHD for the new VM was created from an existing specialized VM, you can use an account from the source virtual machine for RDP.

This example sets the NSG name to myNsg and the RDP rule name to myRdpRule.

$nsgName = "myNsg"

$rdpRule = New-AzureRmNetworkSecurityRuleConfig -Name myRdpRule -Description "Allow RDP" `
    -Access Allow -Protocol Tcp -Direction Inbound -Priority 110 `
    -SourceAddressPrefix Internet -SourcePortRange * `
    -DestinationAddressPrefix * -DestinationPortRange 3389
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $destinationResourceGroup -Location $location `
    -Name $nsgName -SecurityRules $rdpRule

For more information about endpoints and NSG rules, see Opening ports to a VM in Azure using PowerShell.

Create a public IP address and NIC

To enable communication with the virtual machine in the virtual network, you need a public IP address and a network interface.

Create the public IP. In this example, the public IP address name is set to myIP.

$ipName = "myIP"
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $destinationResourceGroup -Location $location `
   -AllocationMethod Dynamic

Create the NIC. In this example, the NIC name is set to myNicName.

$nicName = "myNicName"
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $destinationResourceGroup `
    -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

Set the VM name and size

This example sets the VM name to myVM and the VM size to Standard_A2.

$vmName = "myVM"
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize "Standard_A2"

Add the NIC

$vm = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id

Add the OS disk

Add the OS disk to the configuration using Set-AzureRmVMOSDisk. This example sets the size of the disk to 128 GB and attaches the managed disk as a Windows OS disk.

$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType StandardLRS `
    -DiskSizeInGB 128 -CreateOption Attach -Windows

Complete the VM

Create the VM using New-AzureRMVMthe configurations that we just created.

New-AzureRmVM -ResourceGroupName $destinationResourceGroup -Location $location -VM $vm

If this command was successful, you'll see output like this:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK   

Verify that the VM was created

You should see the newly created VM either in the Azure portal, under Browse > Virtual machines, or by using the following PowerShell commands:

$vmList = Get-AzureRmVM -ResourceGroupName $destinationResourceGroup
$vmList.Name

Next steps

To sign in to your new virtual machine, browse to the VM in the portal, click Connect, and open the Remote Desktop RDP file. Use the account credentials of your original virtual machine to sign in to your new virtual machine. For more information, see How to connect and log on to an Azure virtual machine running Windows.