Assign a vCenter license

An interesting question arrived in my mailbox recently. The user was trying to find out how he could assign a vCenter license. The vSphere API Reference clearly pointed to the UpdateAssignedLicense method.

But the value that should be provided in the entity parameter was a bit of a mystery.  An entity ID for a vCenter was new for me.

UpdateAssignedLicense-parameters

Fyi, the vCenter Server I used for testing was running in evaluation mode.

After a lot of trial and error I discovered the VC’s Uuid was the entity ID the method was expecting.

This is the script that does the license assignment

Annotation:

Line 1: official name of the vCenter. Used to look up the license key.

Line 5: the InstanceUuid is the entity ID for the VC.

Line 7: fetch the VC license that is already loaded with the AddLicense method.

Line 10-23: displays on the console the product name, the product version and all the features.

8 Comments

    iliya

    Hi LucD
    Thanks for the entityID solution’but i have still one issue to solve:
    first i add the licence with addlicense call and this works fine’
    then i call to updateassignedlicense in order to set is as a current licence but it’s not affecting when i open the vshere client i see two licenses and the old one i assigned.

    Michael Scott

    @LucD
    LucD, Thanks again for your help.

    I found a way to make this work. I know this code is a little dirty, but I was in a hurry and was using multiple sources as a reference. This is the code that I am using:

    IF ($NTPhs.Config.Product.Version -lt “4.0.0”) {
    $LicManView.SetLicenseEdition($ESXHostView.MoRef,”esxFull”)
    $LicManView.ConfigureLicenseSource($ESXHostView.MoRef,$LicServerSource)
    }
    ELSE {
    $vcLicName = “vSphere 4 Enterprise”
    $targethostMoRef = (get-VMHost $ESXHost.Name | get-view).MoRef
    $si = Get-View ServiceInstance
    $LicManRef = $si.Content.LicenseManager
    $LicManView = Get-View $LicManRef
    $licAssMan = Get-View $LicManView.LicenseAssignmentManager
    $LicenseMan = Get-View $si.Content.LicenseManager
    $licGetMan = Get-View $LicenseMan.LicenseAssignmentManager
    $LicUse = $licGetMan.QueryAssignedLicenses($targethostMoRef.value)
    $LicKey = ($LicUse | % {$_.AssignedLicense}).LicenseKey
    $LicName = ($LicUse | % {$_.AssignedLicense}).Name

    if ($LicName -eq “Evaluation Mode”) {

    $LicFound = 0
    foreach ($LicKey in $LicManView.Licenses){
    if ($LicKey.Name -eq $vcLicName){
    if (($LicKey.Total – $LicKey.Used) -ge ($ESXHostview.Summary.Hardware.NumCpuPkgs)){
    $licassman.UpdateAssignedLicense($targethostMoRef.value,$LicKey.LicenseKey,$ESXHost.Name)
    $LicFound = 1
    Break # Only for the First License Found
    }
    }
    }
    if ($LicFound -eq 0){
    [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
    [System.Windows.Forms.MessageBox]::Show(“A vSphere Host License could not be found, you may not have enough licenses available to satisfy the license request of this host.”)
    }
    }
    }

    Michael Scott

    @LucD

    I tried this but it did not work. The updateassignedlicense method removes all current licenses and then adds one license. I am trying to find a way to specify that the host should be assigned two licenses.

    do you have any other ideas? Thanks again for your help!

      LucD

      Hi Michael, I have limited web access till Monday.
      Can you hold on for a couple of days ?

    Michael Scott

    @Justin
    I am trying to use the script you are using as an example, but I have modified it a bit. It seems to assign a sing per processor license, but I have to sockets in use on the ESX server. How do I specify that I want 2 licenses assigned to this vSphere host? Any recommendations?

    My version of the script:

    $vcLicName = “vSphere 4 Enterprise”
    $servInst = Get-View ServiceInstance
    $licMgr = Get-View $servInst.Content.licenseManager
    $licAssignMgr = Get-View $licMgr.licenseAssignmentManager
    $ESXUuid = $ESXHostView.Summary.Hardware.Uuid
    foreach ($LicKey in $licMgr.Licenses){
    if ($LicKey.Name -eq “vSphere 4 Enterprise”){
    if (($LicKey.Total – $LicKey.Used) -ge ($ESXHostview.Summary.Hardware.NumCpuPkgs)){
    $licInfo = $licAssignMgr.UpdateAssignedLicense($ESXUuid, $LicKey.LicenseKey, $ESXHost.Name)
    Break # Only for the First License Found
    }
    }
    }

      LucD

      Michael, Can’t you call the UpdateAssignedLicense method in a foreach loop over all the processor blocks ?
      Something like this

      for(1..$ESXHostview.Summary.Hardware.NumCpuPkgs){
      $licInfo = $licAssignMgr.UpdateAssignedLicense($ESXUuid, $LicKey.LicenseKey, $ESXHost.Name)
      }

      Of course provided the license in $LicKey.Name has enough free licenses left.

    Justin

    I came out with a very similar set of PS to set the license for an ESX host.

    $targethostMoRef = (get-VMHost $esxhost | get-view).MoRef
    $si = Get-View ServiceInstance
    $LicManRef = $si.Content.LicenseManager
    $LicManView = Get-View $LicManRef
    $esxLicense = $LicManView.Licenses | where {$_.EditionKey -eq “esxEnterprise”}
    $licassman = Get-View $LicManView.LicenseAssignmentManager
    $licassman.UpdateAssignedLicense($targethostMoRef.value,$esxLicense.LicenseKey,$esxLicense.Name)

      LucD

      Yes, the script for a VC license is practically the same but it was the entity ID that caused the problem in the case of a VC.
      Thanks for including your code.

Leave a Reply

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

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.