|
| 1 | +--- |
| 2 | +title: Attach an existing OS disk to a VM |
| 3 | +description: Create a new Windows VM by attaching a specialized OS disk. |
| 4 | +author: cynthn |
| 5 | +ms.service: virtual-machines |
| 6 | +ms.workload: infrastructure-services |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 03/30/2023 |
| 9 | +ms.author: cynthn |
| 10 | +ms.custom: devx-track-azurepowershell |
| 11 | + |
| 12 | +--- |
| 13 | +# Create a VM from a specialized disk by using PowerShell |
| 14 | + |
| 15 | +**Applies to:** :heavy_check_mark: Windows VMs |
| 16 | + |
| 17 | +Create a new VM by attaching an existing OS disk to a new VM. This option is useful if you have a VM that isn't working correctly. You can delete the VM and then reuse the disk to create a new VM. |
| 18 | + |
| 19 | +> [!IMPORTANT] |
| 20 | +> |
| 21 | +> You can also use the VHD as a source to create an Azure Compute Gallery image. For more information, see [Create an image definition and image version](image-version.md). Customers are encouraged to use Azure Compute Gallery as all new features like ARM64, Trusted Launch, and Confidential VM, are only supported through Azure Compute Gallery. Creating an image instead of just attaching a disk means you can create multiple VMs from the same source disk. |
| 22 | +> |
| 23 | +> When you use a specialized disk to create a new VM, the new VM retains the computer name of the original VM. Other computer-specific information (like the CMID) is also kept and, in some cases, this duplicate information could cause issues. When copying a VM, be aware of what types of computer-specific information your applications rely on. |
| 24 | +
|
| 25 | + |
| 26 | + |
| 27 | +We recommend that you limit the number of concurrent deployments to 20 VMs from a single VHD or snapshot. |
| 28 | + |
| 29 | + |
| 30 | +### [Portal](#tab/portal) |
| 31 | + |
| 32 | + |
| 33 | +Create a snapshot and then create a disk from the snapshot. This strategy allows you to keep the original VHD as a fallback: |
| 34 | + |
| 35 | +1. Open the [Azure portal](https://portal.azure.com). |
| 36 | +2. In the search box, enter **disks** and then select **Disks** to display the list of available disks. |
| 37 | +3. Select the disk that you would like to use. The **Disk** page for that disk appears. |
| 38 | +4. From the menu at the top, select **Create snapshot**. |
| 39 | +5. Choose a **Resource group** for the snapshot. You can use either an existing resource group or create a new one. |
| 40 | +6. Enter a **Name** for the snapshot. |
| 41 | +7. For **Snapshot type**, choose **Full**. |
| 42 | +8. For **Storage type**, choose **Standard HDD**, **Premium SSD**, or **Zone-redundant** storage. |
| 43 | +9. When you're done, select **Review + create** to create the snapshot. |
| 44 | +10. After the snapshot has been created, select **Home** > **Create a resource**. |
| 45 | +11. In the search box, enter **managed disk** and then select **Managed Disks** from the list. |
| 46 | +12. On the **Managed Disks** page, select **Create**. |
| 47 | +13. Choose a **Resource group** for the disk. You can use either an existing resource group or create a new one. This selection will also be used as the resource group where you create the VM from the disk. |
| 48 | +14. For **Region**, you must select the same region where the snapshot is located. |
| 49 | +15. Enter a **Name** for the disk. |
| 50 | +16. In **Source type**, ensure **Snapshot** is selected. |
| 51 | +17. In the **Source snapshot** drop-down, select the snapshot you want to use. |
| 52 | +18. For **Size**, you can change the storage type and size as needed. |
| 53 | +19. Make any other adjustments as needed and then select **Review + create** to create the disk. Once validation passes, select **Create**. |
| 54 | + |
| 55 | + |
| 56 | +After you have the disk that you want to use, you can create the VM in the portal: |
| 57 | + |
| 58 | +1. In the search box, enter **disks** and then select **Disks** to display the list of available disks. |
| 59 | +3. Select the disk that you would like to use. The **Disk** page for that disk opens. |
| 60 | +4. In the **Essentials** section, ensure that **Disk state** is listed as **Unattached**. If it isn't, you might need to either detach the disk from the VM or delete the VM to free up the disk. |
| 61 | +4. In the menu at the top of the page, select **Create VM**. |
| 62 | +5. On the **Basics** page for the new VM, enter a **Virtual machine name** and either select an existing **Resource group** or create a new one. |
| 63 | +6. For **Size**, select **Change size** to access the **Size** page. |
| 64 | +7. The disk name should be pre-filled in the **Image** section. |
| 65 | +8. On the **Disks** page, you may notice that the **OS Disk Type** cannot be changed. This preselected value is configured at the point of Snapshot or VHD creation and will carry over to the new VM. If you need to modify disk type take a new snapshot from an existing VM or disk. |
| 66 | +9. On the **Networking** page, you can either let the portal create all new resources or you can select an existing **Virtual network** and **Network security group**. The portal always creates a new network interface and public IP address for the new VM. |
| 67 | +10. On the **Management** page, make any changes to the monitoring options. |
| 68 | +11. On the **Guest config** page, add any extensions as needed. |
| 69 | +12. When you're done, select **Review + create**. |
| 70 | +13. If the VM configuration passes validation, select **Create** to start the deployment. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +### [PowerShell](#tab/powershell) |
| 76 | + |
| 77 | + |
| 78 | +If you had a VM that you deleted and you want to reuse the OS disk to create a new VM, use [Get-AzDisk](/powershell/module/az.compute/get-azdisk). |
| 79 | + |
| 80 | +```powershell |
| 81 | +$resourceGroupName = 'myResourceGroup' |
| 82 | +$osDiskName = 'myOsDisk' |
| 83 | +$osDisk = Get-AzDisk ` |
| 84 | +-ResourceGroupName $resourceGroupName ` |
| 85 | +-DiskName $osDiskName |
| 86 | +``` |
| 87 | +You can now attach this disk as the OS disk to a new VM. |
| 88 | + |
| 89 | +Create the [virtual network](../virtual-network/virtual-networks-overview.md) and subnet for the VM. |
| 90 | + |
| 91 | +1. 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*. |
| 92 | + |
| 93 | + ```powershell |
| 94 | + $subnetName = 'mySubNet' |
| 95 | + $singleSubnet = New-AzVirtualNetworkSubnetConfig ` |
| 96 | + -Name $subnetName ` |
| 97 | + -AddressPrefix 10.0.0.0/24 |
| 98 | + ``` |
| 99 | + |
| 100 | +2. Create the virtual network. This example 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*. |
| 101 | + |
| 102 | + ```powershell |
| 103 | + $vnetName = "myVnetName" |
| 104 | + $vnet = New-AzVirtualNetwork ` |
| 105 | + -Name $vnetName -ResourceGroupName $destinationResourceGroup ` |
| 106 | + -Location $location ` |
| 107 | + -AddressPrefix 10.0.0.0/16 ` |
| 108 | + -Subnet $singleSubnet |
| 109 | + ``` |
| 110 | + |
| 111 | +
|
| 112 | +To be able to sign in to your VM with remote desktop protocol (RDP), you'll need to have a security rule that allows RDP access on port 3389. In our example, the VHD for the new VM was created from an existing Windows specialized VM, so you can use an account that existed on the source virtual machine for RDP. This example denies RDP traffic, to be more secure. You can change `-Access` to `Allow` if you want to allow RDP access. |
| 113 | +
|
| 114 | +This example sets the network security group (NSG) name to *myNsg* and the RDP rule name to *myRdpRule*. |
| 115 | +
|
| 116 | +```powershell |
| 117 | +$nsgName = "myNsg" |
| 118 | +
|
| 119 | +$rdpRule = New-AzNetworkSecurityRuleConfig -Name myRdpRule -Description "Deny RDP" ` |
| 120 | + -Access Deny -Protocol Tcp -Direction Inbound -Priority 110 ` |
| 121 | + -SourceAddressPrefix Internet -SourcePortRange * ` |
| 122 | + -DestinationAddressPrefix * -DestinationPortRange 3389 |
| 123 | +$nsg = New-AzNetworkSecurityGroup ` |
| 124 | + -ResourceGroupName $destinationResourceGroup ` |
| 125 | + -Location $location ` |
| 126 | + -Name $nsgName -SecurityRules $rdpRule |
| 127 | + |
| 128 | +``` |
| 129 | + |
| 130 | +For more information about endpoints and NSG rules, see [Filter network traffic with a network security group](../virtual-network/tutorial-filter-network-traffic-powershell.md). |
| 131 | + |
| 132 | +To enable communication with the virtual machine in the virtual network, you'll need a [public IP address](../virtual-network/ip-services/public-ip-addresses.md) and a network interface. |
| 133 | + |
| 134 | +1. Create the public IP. In this example, the public IP address name is set to *myIP*. |
| 135 | + |
| 136 | + ```powershell |
| 137 | + $ipName = "myIP" |
| 138 | + $pip = New-AzPublicIpAddress ` |
| 139 | + -Name $ipName -ResourceGroupName $destinationResourceGroup ` |
| 140 | + -Location $location ` |
| 141 | + -AllocationMethod Dynamic |
| 142 | + ``` |
| 143 | + |
| 144 | +2. Create the NIC. In this example, the NIC name is set to *myNicName*. |
| 145 | + |
| 146 | + ```powershell |
| 147 | + $nicName = "myNicName" |
| 148 | + $nic = New-AzNetworkInterface -Name $nicName ` |
| 149 | + -ResourceGroupName $destinationResourceGroup ` |
| 150 | + -Location $location -SubnetId $vnet.Subnets[0].Id ` |
| 151 | + -PublicIpAddressId $pip.Id ` |
| 152 | + -NetworkSecurityGroupId $nsg.Id |
| 153 | + ``` |
| 154 | + |
| 155 | +
|
| 156 | +
|
| 157 | +Set the VM name and size. This example sets the VM name to *myVM* and the VM size to *Standard_A2*. |
| 158 | +
|
| 159 | +```powershell |
| 160 | +$vmName = "myVM" |
| 161 | +$vmConfig = New-AzVMConfig -VMName $vmName -VMSize "Standard_A2" |
| 162 | +``` |
| 163 | + |
| 164 | +Add the NIC. |
| 165 | + |
| 166 | +```powershell |
| 167 | +$vm = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id |
| 168 | +``` |
| 169 | + |
| 170 | + |
| 171 | +Add the OS disk. Add the OS disk to the configuration by using [Set-AzVMOSDisk](/powershell/module/az.compute/set-azvmosdisk). This example sets the size of the disk to *128 GB* and attaches the disk as a *Windows* OS disk. |
| 172 | + |
| 173 | +```powershell |
| 174 | +$vm = Set-AzVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType Standard_LRS ` |
| 175 | + -DiskSizeInGB 128 -CreateOption Attach -Windows |
| 176 | +``` |
| 177 | + |
| 178 | +Create the VM by using [New-AzVM](/powershell/module/az.compute/new-azvm) with the configurations that we just created. |
| 179 | + |
| 180 | +```powershell |
| 181 | +New-AzVM -ResourceGroupName $destinationResourceGroup -Location $location -VM $vm |
| 182 | +``` |
| 183 | + |
| 184 | +If this command is successful, you'll see output like this: |
| 185 | + |
| 186 | +```powershell |
| 187 | +RequestId IsSuccessStatusCode StatusCode ReasonPhrase |
| 188 | +--------- ------------------- ---------- ------------ |
| 189 | + True OK OK |
| 190 | +
|
| 191 | +``` |
| 192 | + |
| 193 | +You should see the newly created VM either in the [Azure portal](https://portal.azure.com) under **Browse** > **Virtual machines**, or by using the following PowerShell commands. |
| 194 | + |
| 195 | +```powershell |
| 196 | +$vmList = Get-AzVM -ResourceGroupName $destinationResourceGroup |
| 197 | +$vmList.Name |
| 198 | +``` |
| 199 | +--- |
| 200 | +**Next steps** |
| 201 | +Learn more about [Azure Compute Gallery](azure-compute-gallery.md). |
0 commit comments