{"id":4362,"date":"2017-05-08T10:33:47","date_gmt":"2017-05-08T08:33:47","guid":{"rendered":"http:\/\/contoso.se\/blog\/?p=4362"},"modified":"2017-06-06T19:17:34","modified_gmt":"2017-06-06T17:17:34","slug":"upload-vhd-and-create-new-vm-with-managed-disk","status":"publish","type":"post","link":"http:\/\/contoso.se\/blog\/?p=4362","title":{"rendered":"Upload VHD and create new VM with managed disk"},"content":{"rendered":"<p>In this post I would like to share scripts and steps I used to create a new Azure VM with managed disks based on a uploaded Hyper-V VHD file. There is a number of things to do before uploading the VHD to Azure. Microsoft Docs has a <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/virtual-machines\/windows\/prepare-for-upload-vhd-image?toc=%2fazure%2fvirtual-machines%2fwindows%2ftoc.json\">good checklist here<\/a> with steps how-to prepare a Windows VM to be uploaded to Azure. Some of the most important things to think about is that the disk must have a fixed size, be in VHD format and the VM must be generation 1. It is also recommended to enable RDP (\u00f0\u0178\u02dc\u0160) and install the <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=394789&amp;clcid=0x409\">Azure VM Agent<\/a>. The overall steps are<\/p>\n<ol>\n<li>Create a Azure storage account<\/li>\n<li>Prepare the server according to the link above<\/li>\n<li>Export the disk in VHD format with fixed size<\/li>\n<li>Build a new VM with the exported disk. This is not required, but can be a good thing to do just to verify that the exported VHD file works before uploaded to Azure<\/li>\n<li>Upload the VHD file<\/li>\n<li>Create a new VM based on the VHD file<\/li>\n<li>Connect to the new VM and verify everything works<\/li>\n<li>Delete the uploaded VHD file<\/li>\n<\/ol>\n<p>The following figure show the storage account configuration were the VHD file is stored<\/p>\n<p><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload-VHD-Create-Storage-Account.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-4363\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload-VHD-Create-Storage-Account-139x300.png\" alt=\"\" width=\"139\" height=\"300\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload-VHD-Create-Storage-Account-139x300.png 139w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload-VHD-Create-Storage-Account.png 310w\" sizes=\"(max-width: 139px) 100vw, 139px\" \/><\/a>The following image show the upload process of the VHD file&#8230; the last image show creation of the new VM<\/p>\n<p><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-Upload-of-VHD-file.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-4364\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-Upload-of-VHD-file-300x127.jpg\" alt=\"\" width=\"300\" height=\"127\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-Upload-of-VHD-file-300x127.jpg 300w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-Upload-of-VHD-file-768x326.jpg 768w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-Upload-of-VHD-file-1024x435.jpg 1024w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-Upload-of-VHD-file.jpg 1176w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-New-VM-Created.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-4365\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-New-VM-Created-300x56.jpg\" alt=\"\" width=\"300\" height=\"56\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-New-VM-Created-300x56.jpg 300w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-New-VM-Created-768x144.jpg 768w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Upload002-New-VM-Created.jpg 925w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The script I used to upload the VHD file<\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<div>Login-AzureRmAccount -SubscriptionId 9asd3a0-aaaaa-4a1c-85ea-4d11111110be5<\/div>\n<div>$localfolderpath = &#8220;C:\\Export&#8221;<br \/>\n$vhdfilename = &#8220;LND-SRV-1535-c.vhd&#8221;<br \/>\n$rgName = &#8220;migration-rg&#8221;<br \/>\n$storageaccount = &#8220;https:\/\/migration004.blob.core.windows.net\/upload\/&#8221;<\/div>\n<div>$localpath = $localfolderpath + &#8220;\\&#8221; + $vhdfilename<br \/>\n$urlOfUploadedImageVhd = $storageaccount + $vhdfilename<br \/>\nAdd-AzureRmVhd -ResourceGroupName $rgName -Destination $urlOfUploadedImageVhd -LocalFilePath $localpath -OverWrite<\/div>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>The script I used to create the new VM. Note that the script connects the new VM to the first subnet on a VNET called CONTOSO-VNET-PRODUCTION. Also note that the size of the VM is set to Standard_A2.<\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<div>Login-AzureRmAccount -SubscriptionId 9asd3a0-aaaaa-4a1c-85ea-4d11111110be5<\/div>\n<div>$vmName = &#8220;LND-SRV-1535&#8221;<br \/>\n$location = &#8220;West Europe&#8221;<br \/>\n$rgName = &#8220;migration-rg&#8221;<br \/>\n$vhdfile = &#8220;LND-SRV-1535-c&#8221;<br \/>\n$vhdsize = &#8220;25&#8221;<br \/>\n$sourceVHD = &#8220;https:\/\/migration004.blob.core.windows.net\/upload\/&#8221; + $vhdfile + &#8220;.vhd&#8221;<\/div>\n<div>### Create new managed disk based on the uploaded VHD file<br \/>\n$manageddisk = New-AzureRmDisk -DiskName $vhdfile -Disk (New-AzureRmDiskConfig -AccountType StandardLRS -Location $location -CreateOption Import -SourceUri $sourceVHD -OsType Windows -DiskSizeGB $vhdsize) -ResourceGroupName $rgName<\/div>\n<div>### Set VM Size<br \/>\n$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize &#8220;Standard_A2&#8221;<\/div>\n<div>### Get network for new VM<br \/>\n$vnet = Get-AzureRMVirtualNetwork -Name CONTOSO-VNET-PRODUCTION -ResourceGroupName CONTOSO-RG-NETWORKING<br \/>\n$ipName = $vmName + &#8220;-pip&#8221;<br \/>\n$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic<br \/>\n$nicName = $vmName + &#8220;-nic1&#8221;<br \/>\n$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id<br \/>\n$vm = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id<\/div>\n<div>### Set disk<br \/>\n$vm = Set-AzureRmVMOSDisk -Name $manageddisk.Name -ManagedDiskId $manageddisk.Id -CreateOption Attach -vm $vm -Windows<\/div>\n<div>### Create the new VM<br \/>\nNew-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm<\/div>\n<\/blockquote>\n<div><\/div>\n<div>Once the second script is completed, the resource group contains a VM, a public IP address, a network adapter and the managed disk. In this example the resource group also contains the storage account.<\/div>\n<p><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Azure-RG.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-4367\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Azure-RG-300x184.jpg\" alt=\"\" width=\"300\" height=\"184\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Azure-RG-300x184.jpg 300w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Azure-RG-768x470.jpg 768w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2017\/05\/Azure-RG.jpg 892w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I would like to share scripts and steps I used to create a new Azure VM with managed disks based on a uploaded Hyper-V VHD file. There is a number of things to do before uploading the VHD to Azure. Microsoft Docs has a good checklist here with steps how-to prepare a &hellip; <a href=\"http:\/\/contoso.se\/blog\/?p=4362\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":4364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[64,13],"tags":[],"_links":{"self":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4362"}],"collection":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4362"}],"version-history":[{"count":4,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4362\/revisions"}],"predecessor-version":[{"id":4370,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4362\/revisions\/4370"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/media\/4364"}],"wp:attachment":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4362"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}