Script Sample - VM Operations - Apply HUB Licensing to Existing VMs
@20aman Dec 07, 2018If you have older VMs in the environment and you wan to leverage Microsoft's Hybrid Use Benefit i.e. HUB licensing then this script can update multiple VMs in your environment in an automated way for you.
NOTE:
- Before you apply the HUB licensing, please make sure whether you are eligible for HUB licensing benefits or not. Refer the official documentation here: Azure Hybrid Benefit
Script Requirements
The script requires you to populate the configurations CSV file. The sample CSV file is also provided along with the script. This configurations file should have the following columns:
- Computer - Name of the VM in Azure
- OSType - Windows or Linux
- ResourceGroupName - Resource Group of the VM
- HUBLicensingNeeded - yes or no. The HUB Licensing is set for only the VMs for which this value is set to yes.
Script Working
In its entirety, the HUB licensing is just a switch for "LicenseType" on the VM. For Windows servers, if it's value is set to "Windows_Server" then this means that you own a Windows Server license and are claiming the HUB Licensing benefits on this VM.
The script applied the HUB licensing in 3 steps. First, it fetches the VM.
$currentVm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $virtualMachineName -ErrorAction Stop
Then it applies the HUB licensing as shown below.
$currentVm.LicenseType = "Windows_Server"
Lastly, it updates the VM object.
Update-AzureRmVM -VM $currentVm -ResourceGroupName $ResourceGroupName
Note: As a best practice, the script also checks whether the HUB Licensing is already applied or not. If the HUB licensing is already there on the VM then the Script does applies it again. There may be small downtime when the VM is updating and therefore a short downtime should be planned when doing this operation.
The script encapsulates all this logic into a reusable function. It also provides the best practices template to invoke this function. Note: Check the comments marked with "ToDo" where you should be making the changes.
You can find more related details in official documentation here: Azure Hybrid Benefit for Windows Server
Location of the Script
You can find this script in GitHub at this location: Apply-HubLicensing.ps1