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

Function for getting report on annotation in VirtualCenter server clusters or particular cluster

I described one way of getting this report in earlier post. If you want to use it as a function… there you go 😉
You can always use get-help greg-get-annotations , if you are in powershell session and want to check the syntax etc..
Report will also include the normal notes section. I’ve tested it in some environments, for example it took 38 sec to complete it in VI where there was ~1500vms, including creation of the csv.

function greg-get-annotations {
<#
.DESCRIPTION
Greg-get-annotations function stores information about annotation fields for vms in given
cluster or in all clusters in VC. It stores the result in an arraylist $vms, you can either create
a csv report from this object or display it on screen
greg-get-annotations |export-csv -NoTypeInformation c:\file1.csv will export it to csv file etc...
greg-get-annotations |format-table VMname,Cluster,CreatedOn,Notes will just display on screen a table with
annotations that include : vm name, its cluster and field "CreatedOn" and Notes

.PARAMETER clustername
Specifies the clustername against wchi report will be built

.EXAMPLE
greg-get-annotations -clustername 'cluster01'|Export-Csv c:\annotation-report.csv
Will procude report on vms that resides in 'cluster01' and store it in csv file

.EXAMPLE
greg-get-annotations -clustername 'cluster01'|ft *
Will procude report on vms that resides in 'cluster01' output it to screen

.EXAMPLE
greg-get-annotations |Export-Csv c:\annotation-report.csv
Will procude report on vms that resides in all clusters and output it to screen

.EXAMPLE
greg-get-annotations
Without specified -clustername switch, it will do report regarding all clusters in VC

.NOTES
AUTHOR: Grzegorz Kulikowski
LASTEDIT: 05/30/2011


#>
param ([string]$clustername)
if(!($clustername)){$clusters=Get-Cluster}else{$clusters=Get-Cluster $clustername}
$VMs=New-Object Collections.ArrayList
foreach ($cluster in $clusters)  {
foreach ($vmview in (get-view -ViewType VirtualMachine -SearchRoot $cluster.id)) {
$vm=New-Object PsObject
Add-Member -InputObject $vm -MemberType NoteProperty -Name VMname -Value $vmview.Name
Add-Member -InputObject $vm -MemberType NoteProperty -Name Notes -Value $vmview.Config.Annotation
Add-Member -InputObject $vm -MemberType NoteProperty -Name Cluster -Value $cluster.Name
foreach ($CustomAttribute in $vmview.AvailableField){
Add-Member -InputObject $vm -MemberType NoteProperty -Name $CustomAttribute.Name -Value ($vmview.Summary.CustomValue | ? {$_.Key -eq $CustomAttribute.Key}).value
}
$VMs.add($vm)|Out-Null
}
}
return $VMs
}

Add-Member -InputObject $vm -MemberType NoteProperty -Name Notes -Value $vmview.Config.Annotation