Networknet archive Blog

Azure: Recreate a VM from a managed OS disk

Originally published

While making changes in Azure, I ran into two scenarios that required some research:

  • Moving a VM from one virtual network to another.
  • Adding a VM to an availability set after it had been created.

I could not find a simple PowerShell or portal command for either task. I found that, after deleting a VM, I could use its OS disk to create a new VM, but the options for Hybrid Use of Windows (AHUB) and an availability set were not available through that route.

Recreate the VM for a virtual network migration

PowerShell42 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#login to Azure account
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName "NAME"

#clean up and remove VM & nic
$rgname = 'Resource Group'
Remove-AzureRmVM -ResourceGroupName  $rgname –Name "vm"  -Force
Remove-AzureRmNetworkInterface –Name "nicname" -ResourceGroup $rgname -force

#set mandatory variables
$loc = 'West Europe'
$vmsize = 'Standard_DS1_v2'
$vmname = 'Server'
$vnetName = 'Virtual-Network'
$subnetName = 'Subnet1'
$nic1 = 'Server_eth0'
#osDiskName - Get-AzureRmDisk for name
$osDiskName = 'Server_OsDisk_1_032ae059cdca4dd88e61ba64bcfae551'
#AvailabilitySetName - Get-AzureRmAvailabilitySet for name
$AvailabilitySetName = 'AS-Servers'

#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 = Get-AzureRmNetworkInterface -Name ($nic1) -ResourceGroupName $rgname;
$nic1Id = $nic1.Id;

#get AvailabilitySetId, set AvailabilitySetId value and add to $vm config
$AvailabilitySetId = Get-AzureRmAvailabilitySet -ResourceGroupName $rgname -Name ($AvailabilitySetName)
$AvailabilitySetId = $AvailabilitySetId.Id
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize -AvailabilitySetId $AvailabilitySetId

#get ManagedDiskId, set ManagedDiskId and add to $vm config
$ManagedDiskId = Get-AzureRmDisk -ResourceGroupName $rgname -DiskName $osDiskName
$ManagedDiskId = $ManagedDiskId.Id
$vm = Set-AzureRmVMOSDisk -VM $vm  -name $osDiskName -CreateOption attach -Windows -ManagedDiskId $ManagedDiskId

#recreate new VM using same OSDisk, new network interface and setting license type to AHUB for Windows Server
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic1Id;
New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm -LicenseType 'Windows_Server' –Verbose

Errors in PowerShell ISE

I ran the script on Windows Server 2012 R2. I encountered the following errors in PowerShell ISE, but did not see them when running the commands in the PowerShell console.

Code11 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
New-AzureRmVM : Required parameter 'osDisk.managedDisk' is missing (null).
ErrorCode: InvalidParameter
ErrorMessage: Required parameter 'osDisk.managedDisk' is missing (null).
StatusCode: 400

ReasonPhrase: Bad Request
New-AzureRmVM : Required parameter 'networkProfile' is missing (null).
ErrorCode: InvalidParameter
ErrorMessage: Required parameter 'networkProfile' is missing (null).
StatusCode: 400
ReasonPhrase: Bad Request