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'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 "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
I did run into couple of issues when running the commands above with PowerShell ISE. I run the PowerShell console and didn't see these errors showing up. I run script from Windows Server 2012 R2 server.
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
Leave a comment