A PowerCLI function to manage VMware vSphere Licenses

As always ( or at least in my world) you should do everything in powershell/powerCLI. Now the turn comes to handling licensing in VMware vSphere.

I searched the web and found some information from Luc Dekens post and from Joe Keegan post. I also used the Onyx VMware fling to get some more functionality.

I decided to do some powershell functions and came up with the following, I have created three functions that can do some magic with your licenses

  • Get-vLicense  – query the vCenter for licenses
  • Add-vLicense – add a license or assign an existing license to a host/vCenter
  • Remove-vLicense – remove a license that is not in use

Of course there might be other things regarding licensing i have forgot but see this as a version 0.1 and that you can give me input on what to add or change

Here you can see how the function get the information:

You have to bare with me and my paintbrush skills, but I cannot show my license keys on the Internet 🙂

I can also use a switch to just show the ones that are not used with

1Get-vLicense -showUnused

I would like to move my hosts to the new license that i have that has unlimited cores

I can also in the Add function just add without assigning the license right now just use the -AddKey switch

And if I want to remove the unused license I  use the following function, It can utilize a pipeline parameter as you see

 

And here is the whole code for the functions:

001function Get-vLicense{
002<#
003.SYNOPSIS
004Function to show all licenses  in vCenter
005  
006.DESCRIPTION
007Use this function to get all licenses in vcenter
008  
009.PARAMETER  xyz 
010  
011.NOTES
012Author: Niklas Akerlund / RTS
013Date: 2012-03-28
014#>
015    param (
016        [Parameter(ValueFromPipeline=$True, HelpMessage="Enter the license key or object")]$LicenseKey = $null,
017        [Switch]$showUnused,
018        [Switch]$showEval
019        )
020    $servInst = Get-View ServiceInstance
021    $licenceMgr = Get-View $servInst.Content.licenseManager
022    if ($showUnused -and $showEval){
023        $licenses = $licenceMgr.Licenses | where {$_.EditionKey -eq "eval" -or $_.Used -eq 0}
024    }elseif($showUnused){
025        $licenses = $licenceMgr.Licenses | where {$_.EditionKey -ne "eval" -and $_.Used -eq 0}
026    }elseif($showEval){
027        $licenses = $licenceMgr.Licenses | where {$_.EditionKey -eq "eval"}
028    }elseif ($LicenseKey -ne $null) {
029        if (($LicenseKey.GetType()).Name -eq "String"){
030            $licenses = $licenceMgr.Licenses | where {$_.LicenseKey -eq $LicenseKey}
031        }else {
032            $licenses = $licenceMgr.Licenses | where {$_.LicenseKey -eq $LicenseKey.LicenseKey}
033        }
034    }
035    else {
036        $licenses = $licenceMgr.Licenses | where {$_.EditionKey -ne "eval"}
037    }
038     
039    $licenses
040}
041 
042function Add-vLicense{
043<#
044.SYNOPSIS
045Add New Licenses to the vCenter license manager
046  
047.DESCRIPTION
048Use this function to add licenses  and assing to either the vcenter or the hosts
049  
050.PARAMETER  xyz 
051    
052.NOTES
053Author: Niklas Akerlund / RTS
054Date: 2012-03-28
055#>
056param (
057    $VMHost ,
058    [Parameter(ValueFromPipeline=$True)]$License = $null,
059    [string]$LicenseKey = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX",
060    [switch]$AddKey
061    )
062    $LicenseMgr = Get-View -Id 'LicenseManager-LicenseManager'
063    $LicenseAssignMgr = Get-View -Id 'LicenseAssignmentManager-LicenseAssignmentManager'
064    if($License){
065        $LicenseKey = $License.LicenseKey
066        $LicenseType = $LicenseMgr.DecodeLicense($LicenseKey)
067    }else{
068        $LicenseType = $LicenseMgr.DecodeLicense($LicenseKey)
069    }
070     
071    if ($LicenseType) {
072        if ($AddKey){
073            $LicenseMgr.AddLicense($LicenseKey, $null)
074        }else{
075            if ($LicenseType.EditionKey -eq "vc"){
076                #$servInst = Get-View ServiceInstance
077                $Uuid = (Get-View ServiceInstance).Content.About.InstanceUuid
078                $licenseAssignMgr.UpdateAssignedLicense($Uuid, $LicenseKey,$null)
079            } else {
080                $key = Get-vLicense -LicenseKey $LicenseKey
081                if($key  -and ($key.Total-$key.Used) -lt (get-vmhost $VMHost | get-view).Hardware.CpuInfo.NumCpuPackages){
082                    Write-Host "Not Enough licenses left"
083                } else{
084                    $Uuid = (Get-VMhost $VMHost | Get-View).MoRef.Value
085                    $licenseAssignMgr.UpdateAssignedLicense($Uuid, $LicenseKey,$null)
086                }
087            }  
088        }
089    }  
090}
091 
092 
093function Remove-vLicense{
094<#
095.SYNOPSIS
096Function to remove a licenses that is not in use in vCenter
097  
098.DESCRIPTION
099Use this function to remove a license
100  
101.PARAMETER  xyz 
102  
103.NOTES
104Author: Niklas Akerlund / RTS
105Date: 2012-03-28
106#>
107param (
108    [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$True, HelpMessage="Enter the key or keyobject to remove")]$License
109    )
110    $LicObj = Get-vLicense $License 
111    if($LicObj.Used -eq 0){
112        $LicenseMgr = Get-View -Id 'LicenseManager-LicenseManager'
113        $LicenseMgr.RemoveLicense($LicObj.LicenseKey)
114    }else{
115        Write-Host " The license is assigned and cannot be removed"
116    }
117}

Comments

Tim
Reply

Hi,

Very good functions !
But I don’t know how to add license for my vCloud Networking & Security extension.
How could I find its UUID ?

Thanks !

paf
Reply

Big thanks! really helps to add licenses, exactly what I was looking for!

nemee
Reply

Amazing script, thanks a lot. I prefer to apply new licensing for all ESXi in datacentrename
get-vmhost -Location datacentrename | ForEach-Object {Add-vLicense $_.name -LicenseKey ******************************}

Michael
Reply

I had some issue with essentials edition, but fixed it replacing line 75 with :
if ($LicenseType.EditionKey -like “vc*”){
to catch vc.essentials.instance type.

Great script ! thanks !

Fred
Reply

Hello,

Thx for this usefull script !
I can’t find the name that we manually set while adding a license.It’s the filed callled “license” in the GUI. This is required for me to find back the correct license to use. Is there anyway to find it with your function ? I checked also the advanced properties from your script but it does not show the field with the name.

Thx

Leave a comment

name*

email* (not published)

website