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

$vcLicName = "vCenter Server 4 Standard"
$servInst = Get-View ServiceInstance
$licMgr = Get-View $servInst.Content.licenseManager
$licAssignMgr = Get-View $licMgr.licenseAssignmentManager
$vcUuid = $servInst.Content.About.InstanceUuid
$vcDisplayName = $servInst.Content.About.Name
$vcLicKey = ($licMgr.Licenses | where {$_.Name -eq $vcLicName}).LicenseKey
$licInfo = $licAssignMgr.UpdateAssignedLicense($vcUuid, $vcLicKey,$vcDisplayName)

# Show the properties of the VC license
$licInfo.Properties | % {
$licFeat = $_
switch($_.Key){
"feature"{
$licFeat.Value | % {
Write-Host $_.Key $_.Value
}
}
Default{
Write-Host $licFeat.Key $licFeat.Value
}
}
}

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.

10 thoughts on “Assign a vCenter license

  1. Pingback: vNiklas Virtualization blog » A PowerCLI function to manage VMware vSphere Licenses

  2. Pingback: VCP5 Blueprint with PowerCLI – Objective 1.2 – Install and Configure VMware ESXi « Infrastructure Adventures

  3. Pingback: PowerCLI: vSphere License Export « Virtu-Al

  4. @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.”)
    }
    }
    }

  5. @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!

  6. @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
    }
    }
    }

    • 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.

  7. 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)

    • 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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>