After vSphere 4.1 hit the streets, my teammates and I decided to move from ESX 4.0 to ESXi 4.1 at all of our locations. With the exception of our primary datacenter, all of our locations were running vSphere 4.0 Standard licensing (read as: no vMotion). In order to quickly evacuate hosts to prepare them for the upgrade to 4.1, it occurred to me that we needed an easier way to set the licensing on those hosts. Since there are no PowerCLI cmdlets at this point that deal directly with licensing, I had to rely on the vSphere SDK to get the job done. For ease of use (and to look nicer), I created functions in that familiar PowerShell verb-noun style.
[powershell]
$vcenter = Connect-VIServer vcenter.domain.com -Protocol https
$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
}
[/powershell]
There are some cool ways that these functions can be used:
Assign a license key to a specific host
[powershell]
$LicKey = Get-LicenseKey -LicName "vSphere 4 Enterprise"
$VMHostId = Get-VMHostId -Name "esxhost.domain.com"
Set-LicenseKey -LicKey $LicKey -VMHostId $VMHostId -Name $null
[/powershell]
Get licensing details based on vCenter parent object, such as a folder named “West”
[powershell]
$vmhosts = Get-VMHost -Location "West"
$details = @()
foreach ($vmhost in $vmhosts) {
$vmhostname = Get-VMHostId $vmhost.name
$detail = Get-License $vmhostname
$details += $detail
}
$details
[/powershell]
h0bbel says
@sixfootdad Oh, yeah. Think so. 😉 http://bit.ly/afMojO
This comment was originally posted onTwitter