Creating a Windows VM from a specialized disk by using PowerShell

  1. Home
  2. Creating a Windows VM from a specialized disk by using PowerShell

Return to AZ-104 Tutorial

Here you’re going to understand how to create Window VM from a specialised disk by using Powershell. This page goes through each and every stage in great detail. So let’s get started.

To do the above, you must first establish a new VM and link a specially managed disc as the OS drive. When you utilize a customized VHD to create a new VM, the new VM keeps the previous VM’s computer name. Other computer-specific data is also stored. This redundant data can, in rare situations, cause problems. However, keep in mind what sorts of computer-specific information your apps rely on while cloning a VM.

You have various options:

  • To begin with, use an existing managed disk. This option is beneficial if you have a VM that isn’t working correctly. You can delete the VM and then reuse the managed disk to create a new VM.
  • Secondly, Upload a VHD
  • Lastly, copy an existing Azure VM by using snapshots

You may also create a new VM from a customised VHD using the Azure interface. So, let’s take a closer look at each choice individually.

Option 1: Using an existing disk

If you had a VM that you deleted and you want to reuse the OS disk to create a new VM, use Get-AzDisk.

$resourceGroupName = 'myResourceGroup'
$osDiskName = 'myOsDisk'
$osDisk = Get-AzDisk -ResourceGroupName $resourceGroupName
-DiskName $osDiskName

Option 2: Uploading a specialized VHD

The VHD can be uploaded from a specific VM developed using an on-premises virtualization solution, such as Hyper-V, or from a VM exported from another cloud.

Preparing the VM

To create a new VM, use the VHD as-is.

  • First of all, prepare a Windows VHD to upload to Azure. Do not generalize the VM by using Sysprep.
  • Next, remove any guest virtualization tools and agents that are installed on the VM (such as VMware tools).
  • Now, make sure the VM is configured to get the IP address and DNS settings from DHCP. This ensures that the server obtains an IP address within the virtual network when it starts up.
Upload the VHD

You may also now upload a VHD directly to a managed drive. See Upload a VHD to Azure Using Azure PowerShell for more.

Option 3: Copy an existing Azure VM

By taking a snapshot of a Windows VM that utilises managed discs and then utilising that snapshot to construct a new managed disc and VM, you may generate a clone of the VM.

You may use azcopy to produce a copy of a disc in another area to copy an existing VM to another region.

Take a snapshot of the OS disk

You may either take a snapshot of the entire VM (all drives) or just a single disc. The following instructions demonstrate how to use the New-AzSnapshot cmdlet to take a snapshot of your VM’s OS disc alone.

First, set some parameters.

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

Getting the VM object.

$vm = Get-AzVM -Name $vmName `
-ResourceGroupName $resourceGroupName

Getting the OS disk name.

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

Creating the snapshot configuration.

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

Taking the snapshot.

$snapShot = New-AzSnapshot -Snapshot $snapshotConfig
-SnapshotName $snapshotName `
-ResourceGroupName $resourceGroupName

Add the -AccountType Premium LRS argument to the New-AzSnapshotConfig command to utilize this snapshot to construct a high-performance VM. This setting causes the snapshot to be saved as a Premium Managed Disk. Premium Managed Disks are more expensive than Standard Managed Disks, therefore make sure you’ll need it before using this option.

Creating a new disk from the snapshot

Using New-AzDisk, construct a managed disc from the snapshot. The disc name in the following example is myOSDisk.

To begin with, create a new resource group for the new Windows VM.

$destinationResourceGroup = 'myDestinationResourceGroup'
New-AzResourceGroup -Location $location `
-Name $destinationResourceGroup

Setting the OS disk name.

$osDiskName = 'myOsDisk'

Creating the managed disk.

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

Creating the new VM

In order to create networking and other Windows VM resources to be used by the new VM.

Creating the subnet and virtual network

Now create the virtual network and subnet for the VM.

  • Create the subnet. This following 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-AzVirtualNetworkSubnetConfig -Name $subnetName
-AddressPrefix 10.0.0.0/24
  • Moving on to create a virtual network. This pattern sets the virtual network name to myVnetName, the location to West US, and the address prefix for the virtual network to 10.0.0.0/16.
$vnetName = "myVnetName"
$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $destinationResourceGroup
-Location $location -AddressPrefix 10.0.0.0/16
-Subnet $singleSubnet
Creating the network security group and an RDP rule

You’ll need a security rule that enables RDP access on port 3389 to sign in to your VM through remote desktop protocol (RDP). Because the new VM’s VHD was built from an existing specialised VM in our example, you can RDP using an account that already existed on the source virtual machine.

This implication sets the network security group (NSG) name to myNsg and the RDP rule name to myRdpRule.

$nsgName = “myNsg”

$rdpRule = New-AzNetworkSecurityRuleConfig -Name myRdpRule -Description "Allow RDP" -Access Allow -Protocol Tcp -Direction Inbound -Priority 110
-SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 $nsg = New-AzNetworkSecurityGroup
-ResourceGroupName $destinationResourceGroup -Location $location
-Name $nsgName -SecurityRules $rdpRule
Create a public IP address and NIC
Practice Test for AZ-104

To enable communication with the Windows virtual machine in the virtual network, you’ll 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-AzPublicIpAddress -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-AzNetworkInterface -Name $nicName -ResourceGroupName $destinationResourceGroup
-Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
-NetworkSecurityGroupId $nsg.Id
Setting the VM name and size

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

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

Add the NIC

$vm = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
Adding the OS disk

Set-AzVMOSDisk is used to add the OS disc to the setup. This example makes the managed disc 128 GB in size and mounts it as a Windows OS drive.

$vm = Set-AzVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType Standard_LRS `
-DiskSizeInGB 128 -CreateOption Attach -Windows
Completing the VM

Create the VM by using New-AzVM with the configurations that we just created.

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

If this command is successful, you’ll see output like this:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
                     True         OK OK

Verifying that the VM was created

Now you’ll be able to see the newly created Windows VM either in the Azure portal under Browse > Virtual machines, or by using the following PowerShell commands.

$vmList = Get-AzVM -ResourceGroupName $destinationResourceGroup
$vmList.Name
Windows VM

Reference: Microsoft Documentation

Return to AZ-104 Tutorial

Menu