«

»

Change ESXi license using PowerCLI

A few days ago a new customer asked me to review the PowerCLI script used for the initial ESXi host configuration. The ESXi hosts are factory installed and the most recent batch of ESXi hosts were all configured with the “VMware vSphere Hypervisor 5 License” license.
Screen Shot 2013-04-15 at 14.09.03
This means the initial ESXi host automatic PowerCLI configuration script we are using doesn’t work because one of the steps includes adding the ESXi host to the vCenter Server. When running:

  • add-vmhost -name $esxi -Location $vcenterfolder -user $esxiuser -password $esxipw –force

The PowerCLI output includes:

  • Add-VMHost : 2013-xx-xx 14:13:35 Add-VMHost The operation for the ent ity Folder-group-h7 failed with the following message: “License not available to perform the operation.”

The vCenter Server “Task Console” gives you this information:
Screen Shot 2013-04-15 at 14.19.46

To fix the problem i added a section in the PowerCLI script to make the ESXi host using the Evaluation license, instead of the preconfigured “VMware vSphere Hypervisor 5 License” license, before adding the ESXi host to vCenter Server.

The below presented script takes care of the following:

  • Change the ESXi host license to “Evaluation License” mode. The string 00000-00000-00000-00000-00000 represents the Evaluation license 
  • Add the ESXi host to the vCenter Server folder specified in the script parameters as $vcenterfolder. In my case ESXi-installation.
  • Set the appropriate license for the ESXi host. In my test case the “VMware vSphere Enterprise Plus” which was a new license used by 0 ESXi hosts at present time.
    Screen Shot 2013-04-13 at 21.56.44

Change the red marked text in the script to your required values.

#————————————————
# Start of script parameters section
#
# vCenter Server, vCenter Server username and vCenter Server password
$vcenter=”172.20.20.21
$vcenteruser=”vcenter@system-domain
$vcenterpw=”notsecret
#
# vCenter Server folder where to place the ESXi host
$vcenterfolder=”ESXi-installation
#
# ESXi host, ESXi host username, ESXi host password
$esxi=”esxi01.home.test
$esxiuser=”root
$esxipw=”notsecret
#
# License name
$licensename=“VMware vSphere 5 Enterprise Plus
#
# End of script parameter section
#——————————————–
#
#
#  Set the ESXi host license to Evaluation license and disconnect from the ESXi host
connect-viserver $esxi -user $esxiuser -password $esxipw
$lm = Get-View -Id ‘LicenseManager-ha-license-manager’
$lm.UpdateLicense(“00000-00000-00000-00000-00000”, $null)
disconnect-viserver $esxi -confirm:$false
#
#
# Connect to vCenter Server
connect-viserver $vcenter -user $vcenteruser -password $vcenterpw
#
#
# Add the ESXi host to the vCenter Server
add-vmhost -name $esxi -Location $vcenterfolder -user $esxiuser -password $esxipw –force
#
#
# Configure the vCenter Server provided license for the ESXi host and disconnect from vCenter Server
$servInst = Get-View ServiceInstance
$licMgr = Get-View $servInst.Content.licenseManager
$licAssignMgr = Get-View $licMgr.licenseAssignmentManager
function Get-LicenseKey($LicName)
{
    $licenses = $licMgr.Licenses | where {$_.Name -eq $LicName}
    foreach ($license in $licenses) {
            if ( (($license.Total – $license.Used) -ne “0”) -or (($license.Total – $license.Used) -lt “0”) )  {
                return $license.LicenseKey
                break
            }
    }
}
function Get-VMHostId($Name)
{
    $vmhost = Get-VMHost $Name | Get-View
    return $vmhost.Config.Host.Value
}
function Set-LicenseKey($VMHostId, $LicKey, $Name)
{
    $license = New-Object VMware.Vim.LicenseManagerLicenseInfo
    $license.LicenseKey = $LicKey
    $licAssignMgr.UpdateAssignedLicense($VMHostId, $license.LicenseKey, $Name)
}
function Get-License($VMHostId)
{
    $details = @()
  $detail = “” |select LicenseKey,LicenseType,Host
    $license = $licAssignMgr.QueryAssignedLicenses($VMHostId)
    $license = $license.GetValue(0)
    $detail.LicenseKey = $license.AssignedLicense.LicenseKey
    $detail.LicenseType = $license.AssignedLicense.Name
    $detail.Host = $license.EntityDisplayName
    $details += $detail
    return $details
}
$LicKey = Get-LicenseKey -LicName $licensename
$VMHostId = Get-VMHostId -Name $esxi
Set-LicenseKey -LicKey $LicKey -VMHostId $VMHostId -Name $null
#
disconnect-viserver $vcenter  -confirm:$false

After running the script i verified that:

  • The ESXi host was licensed with the “VMware vSphere Enterprise Plus” license.
    Screen Shot 2013-04-15 at 14.36.01
  • That two licenses was used from the vCenter Server point of view.
    Screen Shot 2013-04-15 at 14.44.15
  • The ESXi host was added to the vCenter Server folder ESXi-installation.
    Screen Shot 2013-04-13 at 22.05.04

I have used the script (and functions) provided by Damian Karlson to set the vCenter Server based license for the ESXi host. The thread created by Damian can be found here and you can do more stuff with the functions in my above script than just configuring the ESXi host vCenter Server based license.

3 pings

Comments have been disabled.