damiankarlson
Enthusiast
Enthusiast

vSphere Licensing functions

I've been working on these, since there aren't any cmdlets in PowerCLI that do it directly.

$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") {
				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
}

You can do cool things like assign an available license to a vSphere host, or find the assigned license on any/all hosts.

# Get an available license key
$LicKey = Get-LicenseKey -LicName "vSphere 4 Standard"

#Get the host-id
$VMHostId = Get-VMHostId -Name esxhost.domain.com

#Set the key
#Set-LicenseKey -LicKey $LicKey -VMHostId $VMHostId -Name $null

#Find licensing on any/all hosts
$vmhosts = Get-Cluster cluster | Get-VMHost
$details = @()
foreach ($vmhost in $vmhosts) {
	$vmhostname = Get-VMHostId $vmhost.name 
	$detail = Get-License $vmhostname
	$details += $detail
}

Still a work in progress, but I wanted to share what I had so far! Smiley Happy






@sixfootdad

Twitter: @sixfootdad Blog: damiankarlson.com Podcast: professionalvmware.com/brownbags
0 Replies