{"id":2877,"date":"2017-11-23T14:41:26","date_gmt":"2017-11-23T13:41:26","guid":{"rendered":"http:\/\/wordpress.networknet.nl\/?p=2877"},"modified":"2017-11-23T15:52:42","modified_gmt":"2017-11-23T14:52:42","slug":"azure-how-to-recreate-vmcreate-vm-based-on-managed-os-disk","status":"publish","type":"post","link":"https:\/\/www.networknet.nl\/apps\/wp\/archives\/2877","title":{"rendered":"Azure: How to recreate VM\/create VM based on managed OS disk?"},"content":{"rendered":"<p>\n\tTime has come to make fundamental changes in Azure and at that time you will find out that some things are not available (yet?) in the platform. For me I hit couple of things where I needed to do more research and find my way to get the job done.\n<\/p>\n<p>\n\tI have two scenarios where I run into:\n<\/p>\n<ul>\n<li>\n\t\tPerform vNet migration from one virtual network to another\n\t<\/li>\n<li>\n\t\tAdd a VM into a availability set after it is created\n\t<\/li>\n<\/ul>\n<p>\n\tFor both scenarios there is no simple powershell or portal command to execute. At least I didn&#39;t find them. I did figure out when a virtual machine is deleted, the OS disk can be used to create new VM but options for Hybrid Use of Windows (AHUB) and availability set options are not available.\n<\/p>\n<h3>\n\tHow to recreate a VM in Azure to support vNet migration?<br \/>\n<\/h3>\n<pre class=\"brush:powershell\">\r\n#login to Azure account\r\nLogin-AzureRmAccount\r\nSelect-AzureRmSubscription -SubscriptionName &quot;NAME&quot;\r\n\r\n#clean up and remove VM &amp; nic\r\n$rgname = &#39;Resource Group&#39;\r\nRemove-AzureRmVM -ResourceGroupName  $rgname &ndash;Name &quot;vm&quot;  -Force\r\nRemove-AzureRmNetworkInterface &ndash;Name &quot;nicname&quot; -ResourceGroup $rgname -force\r\n\r\n#set mandatory variables\r\n$loc = &#39;West Europe&#39;\r\n$vmsize = &#39;Standard_DS1_v2&#39;\r\n$vmname = &#39;Server&#39;\r\n$vnetName = &#39;Virtual-Network&#39;\r\n$subnetName = &#39;Subnet1&#39;\r\n$nic1 = &#39;Server_eth0&#39;\r\n#osDiskName - Get-AzureRmDisk for name\r\n$osDiskName = &#39;Server_OsDisk_1_032ae059cdca4dd88e61ba64bcfae551&#39;\r\n#AvailabilitySetName - Get-AzureRmAvailabilitySet for name\r\n$AvailabilitySetName = &#39;AS-Servers&#39;\r\n\r\n#create new network interface and set NicId value \r\n$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgname -Name $vnetName\r\n$subnetId = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $subnetName\r\n$subnetId = $subnetId.Id\r\n$nic = New-AzureRmNetworkInterface -Name $nic1 -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId\r\n$nic1 = Get-AzureRmNetworkInterface -Name ($nic1) -ResourceGroupName $rgname;\r\n$nic1Id = $nic1.Id;\r\n\r\n#get AvailabilitySetId, set AvailabilitySetId value and add to $vm config\r\n$AvailabilitySetId = Get-AzureRmAvailabilitySet -ResourceGroupName $rgname -Name ($AvailabilitySetName)\r\n$AvailabilitySetId = $AvailabilitySetId.Id\r\n$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize -AvailabilitySetId $AvailabilitySetId\r\n\r\n#get ManagedDiskId, set ManagedDiskId and add to $vm config\r\n$ManagedDiskId = Get-AzureRmDisk -ResourceGroupName $rgname -DiskName $osDiskName\r\n$ManagedDiskId = $ManagedDiskId.Id\r\n$vm = Set-AzureRmVMOSDisk -VM $vm  -name $osDiskName -CreateOption attach -Windows -ManagedDiskId $ManagedDiskId\r\n\r\n#recreate new VM using same OSDisk, new network interface and setting license type to AHUB for Windows Server\r\n$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic1Id;\r\nNew-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm -LicenseType &#39;Windows_Server&#39; &ndash;Verbose <\/pre>\n<div>\n\t&nbsp;\n<\/div>\n<div>\n\tI did run into couple of issues when running the commands above with PowerShell ISE. I run the PowerShell console and didn&#39;t see these errors showing up. I run script from Windows Server 2012 R2 server.\n<\/div>\n<pre>\r\nNew-AzureRmVM : Required parameter &#39;osDisk.managedDisk&#39; is missing (null).\r\nErrorCode: InvalidParameter\r\nErrorMessage: Required parameter &#39;osDisk.managedDisk&#39; is missing (null).\r\nStatusCode: 400\r\n\r\nReasonPhrase: Bad Request\r\nNew-AzureRmVM : Required parameter &#39;networkProfile&#39; is missing (null).\r\nErrorCode: InvalidParameter\r\nErrorMessage: Required parameter &#39;networkProfile&#39; is missing (null).\r\nStatusCode: 400\r\nReasonPhrase: Bad Request<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Time has come to make fundamental changes in Azure and at that time you will find out that some things are not available (yet?) in the platform. For me I hit couple of things where I needed to do more research and find my way to get the job done. I have two scenarios where I run into: Perform vNet migration from one virtual network to another Add a VM into a availability set after it is created For both scenarios there is no simple powershell or portal command to execute. At least I didn&#39;t find them. I did figure out when a virtual machine is deleted, the OS disk can be used to create new VM but options for Hybrid Use of Windows (AHUB) and availability set options are not available. How to recreate a VM in Azure to support vNet migration? #login to Azure account Login-AzureRmAccount Select-AzureRmSubscription -SubscriptionName &quot;NAME&quot; #clean up and remove VM &amp; nic $rgname = &#39;Resource Group&#39; Remove-AzureRmVM -ResourceGroupName $rgname &ndash;Name &quot;vm&quot; -Force Remove-AzureRmNetworkInterface &ndash;Name &quot;nicname&quot; -ResourceGroup $rgname -force #set mandatory variables $loc = &#39;West Europe&#39; $vmsize = &#39;Standard_DS1_v2&#39; $vmname = &#39;Server&#39; $vnetName = &#39;Virtual-Network&#39; $subnetName = &#39;Subnet1&#39; $nic1 = &#39;Server_eth0&#39; #osDiskName &#8211; Get-AzureRmDisk for name $osDiskName = &#39;Server_OsDisk_1_032ae059cdca4dd88e61ba64bcfae551&#39; #AvailabilitySetName &#8211; Get-AzureRmAvailabilitySet for name $AvailabilitySetName = &#39;AS-Servers&#39; #create new network interface and set NicId value $vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgname -Name $vnetName $subnetId = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $subnetName $subnetId = $subnetId.Id $nic = New-AzureRmNetworkInterface -Name $nic1 -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId $nic1 = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[839],"tags":[844,841,42,843,246,845],"class_list":["post-2877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","tag-ahub","tag-azure","tag-powershell","tag-recreate-vm","tag-vm","tag-vnet-migration"],"_links":{"self":[{"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/posts\/2877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/comments?post=2877"}],"version-history":[{"count":4,"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/posts\/2877\/revisions"}],"predecessor-version":[{"id":2881,"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/posts\/2877\/revisions\/2881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/media\/2873"}],"wp:attachment":[{"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/media?parent=2877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/categories?post=2877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.networknet.nl\/apps\/wp\/wp-json\/wp\/v2\/tags?post=2877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}