Get-LicenseReport to obtain informations regarding licenses within Virtual Center


If you need to create reports regarding licenses usage you can the below script. It gathers all licensing information for virtual center to which you are currently connected. Before running this function make sure you are connected to one single virtual center instance. Please run this function within PowerCLI console, as for now i haven’t put check/loading vmware snappin

Function Get-LicenseReport
{
  <#

   .Synopsis

    Get licensing report from currently connected Virtual Center

   .Description

    This function returns licensing information from the currently connected 
	Virtual Center server. It can output to screen or to html file using the
	ConvertTo-HTML option.
	PreContent of html report can be modified in source code.
	Variable $PreContent describes what is written before generating 
	the table.

   .Example

    Get-LicenseReport

    Will create licenses usage report from currently connected VirtualCenter
	server and output to screen

   .Example

    Get-LicenseReport -htmlfile "C:\licensereport.html"

    Will create licenses usage report from currently connected VirtualCenter
	server and output to file "C:\licensereport.html"
	
	Get-LicenseReport -htmlfile "C:\licensereport.html" -Name "Grzegorz 
	Kulikowski" -Email "myemail@web.com" -CustomerNumber "123123"

    Will create licenses usage report from currently connected VirtualCenter
	server and output to file "C:\licensereport.html" and fill out predefined
	field within report such as name, email, vmware customer number

   .Parameter CustomerNumber

    If you need to specifi additional field like Customer Number
	
	.Parameter Name

    If you need to specify name of the person who creates the report
	
	.Parameter Email

    If you need to specify e-mail of the person who creates the report
	
   .Parameter htmlfile

    Specifies the file name to which html version report will be written

   .Notes

    NAME:  Get-LicenseReport

    AUTHOR: Grzegorz Kulikowski

    LASTEDIT: 08/13/2012
	
	SPECIAL THANKS: Luc Dekens, Robert van den Nieuwendijk, Yasen Kalchev
	CSS TABLE: http://www.textfixer.com/resources/css-tables.ph
	
	NOT WORKING ? #powercli @ freenode.irc.net / 

   .Link

    https://psvmware.wordpress.com

 #>
param (
[string]$htmlfile,
[string]$CustomerNumber,
[string]$Name,
[string]$Email
)
$Head=@"
<Title>License Report for $($global:DefaultVIServer.name)</Title>
 <Style type="text/css">
  P.myinfo {text-align: Left}
  body {
   margin-left: 20%;
   margin-right: 20%;
   padding-top: 30px;
   padding-bottom: 40px;
   color: #2E2E2E;
   background-color: #E6E6E6 }
   
  table.gridtable {
	font-family: verdana,arial,sans-serif;
	font-size:11px;
	color:#333333;
	border-width: 1px;
	border-color: #666666;
	border-collapse: collapse;
}
  table.gridtable th {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #dedede;
}
  table.gridtable td {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #ffffff;
}
 </Style>
"@
 
$PreContent=@"
<center>
<p><h1>License Report</h1></p>
<p class=myinfo>VMware License Review – for VC:<b> $($global:DefaultVIServer.name) </b></br>
Customer Number:<b> $CustomerNumber </b></br>
Name of Person completing this form: <b>$Name</b></br>
Email of Person completing this form: <b>$Email</b></br>
Date: <b> $(get-date) </b></br></br</p>
"@

if(-not ($global:DefaultVIServer) ) { 
	Write-Host -ForegroundColor Red -BackgroundColor Black "You are not connected to any VirtualCenter server. Please connect manually."
	Write-Host -ForegroundColor Yellow -BackgroundColor Black "hint: Connect-VIServer -server myVC.domain.biz -Credential (Get-Credential)"
	break
}
$param = @($null)
$ServiceInstance = Get-View ServiceInstance
$LicenseManager= Get-view $ServiceInstance.Content.LicenseManager
$LicenseAssignmentManager= Get-View $LicenseManager.LicenseAssignmentManager
$LicensesArrayList=New-Object System.Collections.ArrayList 
$LicenseAssignmentManager.GetType().GetMethod("QueryAssignedLicenses").Invoke($LicenseAssignmentManager,$param) |Foreach-Object{
$License=New-Object -TypeName PsObject -Property @{ProductName=$null;Version=$null;Edition=$null;DisplayName=$null;PhysProcessors=$null;CoresPerCpu=$null}
$License.ProductName= ($_.Properties.GetEnumerator()|?{$_.Key -eq 'ProductName'}).Value
$License.Version= ($_.Properties.GetEnumerator()|?{$_.Key -eq 'ProductVersion'}).Value
$License.Edition= $_.AssignedLicense.Name
$License.DisplayName=$_.EntityDisplayName
    if($_.AssignedLicense.EditionKey -like "vco*"){
			$License.DisplayName+=" Used: $($_.AssignedLicense.Used) from: $($_.AssignedLicense.Total)"
		}
	if($_.EntityId -like "host-*"){
		$HostView=Get-View -Id  "HostSystem-$($_.EntityID)"
		$License.PhysProcessors=$HostView.Hardware.CpuInfo.NumCpuPackages
		$License.CoresPerCpu=$hostview.Hardware.CpuInfo.NumCpuCores/$HostView.Hardware.CpuInfo.NumCpuPackages
	}
		else {
			$License.PhysProcessors="N/A"
			$License.CoresPerCpu="N/A"
		}
$LicensesArrayList.Add($License)|Out-Null
}
if($htmlfile){
	($LicensesArrayList | Select-Object ProductName,Version,Edition,DisplayName,CoresPerCpu,PhysProcessors | ConvertTo-Html -PreContent $PreContent -Head $Head ) -Replace '<table>','<table class="gridtable">' |Out-File $htmlfile
	}
	else{
		return $LicensesArrayList
	}
}

I would like to thank Luc Dekens, Robert van den Nieuwendijk, Yasen Kalchev for their support while i was trying get all the information which was needed in order to obtain all data from Virtual Center, as this one was bit tricky 😉

get-licensereport image

get-licensereport image


While writing this script it was very difficult to pass $null to QueryAssignedLicenses method, i mean it was impossible ;). Why did i have to query like that ?

$LicenseAssignmentManager.GetType().GetMethod("QueryAssignedLicenses").Invoke($LicenseAssignmentManager,$param)

http://communities.vmware.com/message/2096977#2096977 Yasen Kalchev explains why do we have to use this approach in order to get ALL licensing information from virtual center.
By the way it’s very easy to obtain exactly the same result if you will point your web browser directly to
https://your-vc-address.biz/?moid=LicenseAssignmentManager&method=queryAssignedLicenses
And leave the EntityID parameter as empty.
What i have also learned while writing this script is that EntityID for VC is kept in

(Get-View ServiceInstance).Content.About.InstanceUuid

And for all esx/i hosts the id is the Value of its MoRef for example HostSystem-host-123 . So EntityID in this case would be the ‘host-123’ part.
If you want to change the content of the html report just edit contents of PreContent/Head variables. If you want to change the look of the html report , you have to edit css style. If you will modify the css table style, make sure you will also change the style name in the -replace section as well, because by default ConvertTo-Html creates a table tag without any style.
Enjoy!

:Update
I have added functionality to get-LicenseReport. If it will discover VC OPS, it will also put into displayname TOTAL and USED numbers for that license.

Advertisements

2 thoughts on “Get-LicenseReport to obtain informations regarding licenses within Virtual Center

  1. Hi Guys,

    Great script. I cant seem to export to html or csv.

    I am following the script as of the above but nothing seems to export or appear in powercli. I am a novice here so apologies 🙂

    Many thanks

    Rhys

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s