Introduction
Assigning and updating licenses for VMware vSphere components – such as ESXi or vCenter servers – is not something anybody is looking forward to, and I think every admin who had ever been dealing with managing an extensive infrastructure will agree. Naturally, it all is basically effortless in theory: you just have to open the vSphere Client, and here we go! However, when there are too many active hosts, a simple license assignment can turn into a bit of a headache.
PowerCLI: A Decent Alternative
That’s why it can appear impressive how fast and straightforward the process is with PowerCLI. Let’s take a look!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
## set vCenter credentials $vCenter = "FQDN of your vCenter" $User= "Username" $Password= "Password unmasked" $License = "License Key" ## Convert the password to connect $EncryptedPassword = ConvertTo-SecureString -String "$Password" -AsPlainText -Force ## Set the credentials or you can alternatively request them using the interactive mode with $Credential = get-credential $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $EncryptedPassword #Connect to the vCenter Disconnect-VIServer -confirm:$false -ErrorAction SilentlyContinue Write-Host "Now connecting to $vCenterServer" $vCenter = Connect-VIServer -Server $vCenterServer -Credential $Credential ## Assign/update license key for this particular vCenter: $LM = get-view($vCenter.ExtensionData.content.LicenseManager) $LM.AddLicense($License,$null) $LAM = get-view($LicenseManager.licenseAssignmentManager) $LAM.UpdateAssignedLicense($vCenter.InstanceUuid,$License,$Null) |
You can also switch this scenario up a little if you have more keys to assign or update:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
$licenses = "key1","key2,"key3","key4" ## Adding more licenses to the existing licenses $LicenseManager = get-view ($vc.ExtensionData.content.LicenseManager) $existinglicenses = $LicenseManager.licenses.licensekey $licensestoadd = $licenses | where-object {$_ -notin $existinglicenses} foreach ($license in $licensestoadd) { write-host"Adding license"+$license $LicenseManager.AddLicense($license,$null) } ## Create the keys for vCenter version $majorversion = $vc.version.split(".")[0] $vcenterlicense = ($LicenseManager.licenses | where {$_.name -like "*vCenter*" -and $_.name -like "*$majorversion*"} | select -first 1).licensekey ## Update license for this particular vCenter instance: $LicenseAssignmentManager = get-view ($LicenseManager.licenseAssignmentManager) $LicenseAssignmentManager.UpdateAssignedLicense($vCenter.InstanceUuid,$vcenterlicense,$Nul) |
In case you want to add/update the VMware ESXi license, there’s a path:
1 2 3 4 5 6 7 8 9 |
## Find host by name $vmhost = Get-VMHost -Name Host ## Add a license key to it Set-VMHost -VMHost $vmhost -LicenseKey YXXXXX-XXXXX-XXXXX-XXXXX-XXXXX |
If you merely want to add the VMware ESXi license key to all the hosts in the cluster, it goes even simpler:
1 |
get-cluster ClusterName | get-vmhost | set-vmhost -LicenseKey XXXXX-XXXXX-XXXXX-XXXXX-XXXXX |
In order to add the Evaluation-key for ESXi, just write it as 00000-00000-00000-00000-00000.
If you want to check up on the existing license keys, here’s a command:
1 |
Get-VMHost | Select Name, LicenseKey |
If you’d like to add a key which capacity is lower than the licenses in use, you would be able to, even though Web Client/vSphere Client will notify you that the license capacity is overused:
If such a situation occurred, you should simply buy more licenses, get a new key, and update all the licenses in your data center.
Speaking of which, if you want to know how many licenses exactly there are in the said data center and when do they expire, here’s a very simple script from vmwarediary.com. You are supposed to get a table like this:
However, in case you need something more serious, well, then, VMware has got something for you, more precisely – vSphere Software Asset Management Tool utility:
Conclusions
As you can see, this algorithm is relatively straightforward and not at all tedious. I hope this little lifehack can help you!