Convert VM’s to use Azure Hybrid Benefit for Windows Server with Azure PowerShell

As most people we always want to drop costs in our IT Infrastructure, with the new Azure Hybrid Benefit for Windows Server this is another way to save money.

Azure Hybrid Benefit for Windows Server allows you to use your on-premises Windows Server licenses and run Windows virtual machines on Azure at a reduced cost. This has been added to the Azure Resource Manager VM deployment process. You can historically go back through your current VM’s and utilise these savings if you are applicable.

Be aware that some VM sizes are not supported, so you may run into this issue.

# To login to Azure Resource Manager
Login-AzureRmAccount

# To select a default subscription for your current session
Set-AzureRmContext -SubscriptionId “<Subscription-ID>”

$VMs = Get-AzureRMVM | Where-Object {$_.LicenseType -eq “”}

foreach ($VM in $VMs) {

$vm.LicenseType = “Windows_Server”
Update-AzureRmVM -VM $VM -ResourceGroupName $VM.ResourceGroupName

}

Move an Azure IaaS VM between vNets

Recently i had a requirement to move a number of VM’s from one Azure vNet to another. This isnt possible in the current Azure Portal (although hopefully is coming based on the comments on the Azure Advisory forums – https://feedback.azure.com/forums/34192–general-feedback), therefore you will need to use PowerShell to do this.

Warning: This action does incur downtime, the method will remove the VM, keeping the disk and create a new VM attaching the kept disk. The disk should keep any installed applications, features or roles.

<# 

Author: HP, July 2017.

 
 

Move a VM that has un-managed disks between vNets: 

 
 

1. Delete VM from the portal. 

2. Create a NIC in the new vNet. 

3. Get the NIC name and add it to variable: $NIC1. 

4. Ensure all other variables are filled out 

5. Ensure that the correct subscription is your default with (Get-AzureRmContext).Subscription.  

   If wrong set with Set-AzureRmContextSubscriptionName “SUBID” –TenantId “TENANTID” 

6. Run below script. 

 
 

#> 

 
 

# To login to Azure Resource Manager 

Login-AzureRmAccount 

          

# To view all subscriptions for your account 

#Get-AzureRmSubscription 

          

# To select a default subscription for your current session 

#Get-AzureRmSubscriptionSubscriptionID “SUBID” | Select-AzureRmSubscription 

          

$RGName = “Resource_Group_Name 

$VMLocation = “VM_Location 

$VMSize = “Enter_VM_Size 

$VMName = “VM_Name 

$VM = New-AzureRmVMConfigVMName $VMNameVMSize $VMSize; 

           

$NIC1 = Get-AzureRmNetworkInterface -Name (“VM_Name“) –ResourceGroupName $RGName; 

$NIC1Id = $NIC1.Id; 

#Uncomment if deploying with Multiple NICs 

#$NIC2 = Get-AzureRmNetworkInterface -Name (“NIC2Name”) –ResourceGroupName $RGName; 

#$NIC2Id = $NIC2.Id; 

           

$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC1Id; 

#Uncomment if deploying with Multiple NICs 

#$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC2Id;              

           

$osDiskName = “Disk_Name 

$osDiskVhdUri = “Enter_VHD_URL.vhd 

           

$VM = Set-AzureRmVMOSDisk -VM $VM –VhdUri $osDiskVhdUri -name $osDiskNameCreateOption attach -Windows 

           

New-AzureRmVMResourceGroupName $RGName -Location $VMLocation -VM $VM -Verbose 

In total this will take around 20 minutes a VM. There are ofcourse some things to be mindful of, and i will list them below:

  • You will need to reconfigure your VM Extensions to any VM’s you move.
  • NIC’s in the new vNet will need to be re-created.
  • You will need to have necessary permissions to the relevant subscriptions to move resources.
  • Always test a move pre doing this in your live environment.