How To Apply vSphere Licensing With PowerCLI

Save to My DOJO

How To Apply vSphere Licensing With PowerCLI

Table of contents

Licensing can be a pain to manage with VMware, especially in large environments where you’re constantly adding and removing ESXi host nodes from clusters. I will be honest, I have seen a few production nodes get disconnected from VCenter due to the “evaluation” mode expiring. Luckily with PowerCLI, we have the Get-LicenseDataManager cmdlet that allows us to manipulate the licensing in our VCenter environment so that we can configure objects such as datacenters, clusters, and datacenter folders to automatically apply vSphere licensing whenever a host gets added to the object.

In this Article:

  • Getting Started with Get-LicenseDataManager
  • Adding a License to a Datacenter Object
  • Removing Licenses
  • Replacing Licenses

Getting Started with Get-LicenseDataManager

First, we need to connect to our VCenter environment using the following syntax in PowerCLI:

Connect-VIServer -Server 192.168.0.9

Next, we want to capture our licensing data into a variable so we run Get-LicenseDataManager and set the captured information to the $LicenseData variable. When we explore the contents of our variable we can see the UID:

$licensedata = Get-licensedataManager

Now, the way this cmdlet works is a little different than your traditional PowerCLI cmdlets. In order to query license information or update licensing information, we need to utilize the available methods of the object. In PowerShell, a “method” is a set process that allows you to perform an action against the object. To see all the available methods that we can run against our $LicenseData variable we simply use the Get-Member cmdlet and pipe our variable to it:

$LicenseData | Get-Member

We now get a picture of all the method’s associated with our $LicenseData object. The ones that are key players are the following:

QueryAssociatedLicenseData – Retrieves license data associated with whatever object specified such as a data centers or ESXi host.

QueryEntityLicenseData – Retrieves all entity license data associations. We would run this to collect a list of all license key’s we have applied to any VCenter objects.

UpdateAssociatedLicenseData– Updates license data for objects like datacenters or ESXi hosts. We will use this method to actually apply licenses or remove them from objects.

Adding a License to a Datacenter Object

We can add a license to a datacenter object in our vCenter environment, so that any ESXi hosts that are added to that datacenter, will automatically get the license key. This works extremely well with environments that have a single large license key. We start off from the previous steps above, get connected to vCenter with PowerCLI and get our $licensedata variable by using the Get-licensedataManager cmdlet. Then we need to create our License Data object in PowerShell:

$licenseInfo = New-Object Vmware.VimAutomation.License.Types.LicenseData

Next, we need to create a license key object to store inside our license data object that we just created:

$licenseKeyEntry = New-Object Vmware.VimAutomation.License.Types.LicenseKeyEntry

Now, we have to specify the type ID for our license key object as “VMware-Vsphere”:

$licenseKeyEntry.TypeId = "vmware-vsphere"

Finally, we can input our license key into the license key object property:

$licenseKeyEntry.LicenseKey = "INSERTLICENSEKEY"

Now we are done with creating our license key object. We will add it into our license data object that we initially created:

$licenseInfo.LicenseKeys += $licenseKeyEntry

Since I want to apply this license to the LukeLab datacenter in my VCenter environment, I will capture the object in PowerShell by creating a variable and using the Get-Datacenter cmdlet and the -name parameter:

$hostContainer = Get-Datacenter -Name LukeLab

Lastly, we will use the UpdateAssociatedLicenseData method to apply the license to our LukeLab datacenter object:

$licenseData.UpdateAssociatedLicenseData($hostContainer.Uid, $licenseInfo)

We can check our change by using the QueryEntityLicenseData method to check for all assigned licenses:

$licensedata.QueryEntityLicenseData()

Now we can test this by adding a new host to the LukeLab datacenter object. The license should be assigned automatically. So I will use the following syntax to add a new host to my LukeLab datacenter object in my VCenter environment:

Add-VMhost -Name 192.168.0.21 -Location LukeLab

Now when I add the host, the  license key is automatically assigned to the host:

Removing Licenses

If we wanted to remove a license from an entity such as a datacenter, host, or cluster, we could use the following syntax. In my example I will remove the license from the “LukeLab” datacenter object by specifying the $null variable instead of an actual license key:

$hostContainer = Get-Datacenter -Name LukeLab

$licenseData.UpdateAssociatedLicenseData($hostContainer.Uid, $null)

Then when we run the following syntax we get no results indicating our license has been removed:

$licensedata.QueryEntityLicenseData()

Replacing Licenses

What happens if I reach the limit of license? The license will still be applied, but VCenter will flag that particular license as overused:

You could then buy a new license and combine it with your current license and then update the datacenter with the new one by following the same steps used to add a license to the cluster originally. The new license will simply overwrite the old one. Then you will have to switch over your hosts to the new license by using the following syntax:

get-vmhost -name * -location LukeLab | Set-VMhost -LicenseKey 00000-00000-00000-00000-00000

Depending on your environment, managing VMware licenses can be quite the chore, however, PowerCLI gives us some pretty useful options to help with this. Assigning a bulk license key is super useful for automating licensing when using Autodeploy, and can also really come in handy if you’re running an environment where the nodes are being rebuilt all the time.

Let me know in the comments below how you use bulk licensing!

[the_ad id=”4738″][thrive_leads id=’18673′]

Altaro VM Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

Leave a comment

Your email address will not be published. Required fields are marked *