VMware Cloud Community
thiag2011
Enthusiast
Enthusiast

To fetch license details allocated to different vcenter

Hi all,

I would like to find out if any specific license is allocated to two different vcenters that are not in linked mode.

Could anyone help by providing a script for the same.

Thanks in advance.

18 Replies
LucD
Leadership
Leadership

Try something like this, it will show for each license key in which vCenters it is used.

You need to connect to all the vCenters before running the script

&{foreach($vc in $global:DefaultVIServers){

  $si = Get-View ServiceInstance -Server $vc

  $licMgr = Get-View -Id $si.Content.LicenseManager

  $asgLicMgr = Get-View -Id $licMgr.LicenseAssignmentManager

  foreach($esx in Get-VMHost -Server $vc){

    $licAssigned = $asgLicMgr.QueryAssignedLicenses($esx.ExtensionData.MoRef.Value)

    $esx | Select @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$_.Name.Split('.')[0]}},

            @{N='LicenseKey';E={$licAssigned[0].AssignedLicense.LicenseKey}}

  }

}} | Group-Object -Property LicenseKey | %{

    $vcs = $_.Group | Select -ExpandProperty vCenter | Sort-Object -Unique

    Write-Output "License Key $($_.Name) is used in vCenters: $([string]::Join(',',$vcs))"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

thiag2011
Enthusiast
Enthusiast

Thank you Luc.. It works, am able to get all the licenses and its assigned vcenters.

But Can I get only the entry of  license key that is assigned in two different vcenters.

Also it will be helpful if you could hide the content of license key like..

2DG45-xxxxx-xxxxx-xxxxx-FD53E

0 Kudos
LucD
Leadership
Leadership

Try something like this

&{foreach($vc in $global:DefaultVIServers){

  $si = Get-View ServiceInstance -Server $vc

  $licMgr = Get-View -Id $si.Content.LicenseManager

  $asgLicMgr = Get-View -Id $licMgr.LicenseAssignmentManager

  foreach($esx in Get-VMHost -Server $vc){

    $licAssigned = $asgLicMgr.QueryAssignedLicenses($esx.ExtensionData.MoRef.Value)

    $esx | Select @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$_.Name.Split('.')[0]}},

            @{N='LicenseKey';E={$licAssigned[0].AssignedLicense.LicenseKey}}

  }

}} | Group-Object -Property LicenseKey |

    where{($_.Group | Select -ExpandProperty vCenter | Sort-Object -Unique).Count -gt 1} | %{

    $vcs = $_.Group | Select -ExpandProperty vCenter | Sort-Object -Unique

    Write-Output "License Key $(($_.Name.split('-')[0,4] -join '-xxxxx-xxxxx-xxxxx-')) is used in vCenters: $([string]::Join(',',$vcs))"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

thiag2011
Enthusiast
Enthusiast

Thanks a lot LucD...

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi LucD,

I have executed the script after connecting to some 5 to 6 vcenters.

The output is showing some wrong values.

It displays as, some 3 to 4  license is being used in two different vcenters, I checked manually but it is not the case ( it is assigned to only one vcenter).

So I have rerun the script, this time it brings some other licenses stating it is assigned to two or more vcenters ( but  this is also false, as I checked manually)

Can you please advice.

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi,

Any suggestion to solve the issue please...

0 Kudos
LucD
Leadership
Leadership

First you should check the content of $global:defaultviservers.

Are you connected to all vCenter when you run the script ?

When you run the script only connected to 1 vCenter, the one where you see the wrong licenses being reported, do you also see the incorrect assignments ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thiag2011
Enthusiast
Enthusiast

Q) First you should check the content of $global:defaultviservers.

    Are you connected to all vCenter when you run the script ?

A) Yes, first i ran disconnect-viserver * and removed all existing connections.

Then connected to 6 vcenters one after other, then i executed the script.

Q) When you run the script only connected to 1 vCenter, the one where you see the wrong licenses being reported, do you also see the incorrect assignments ?

Eg : Consider the output

MM45T-XXXXX-XXXXX-XXXXX-KAL45 is assigned to vcenters abcd123, bcdh456,try678

I manually checked by exporting the licenses of vcenters  and found that this license was not present in abcd123

but it was actually shared between the vcenters bcdh456 and try678

And one more point is, i get different outputs ( mentioning different license shared between vcenters, though it is wrong)  when I execute the script multiple times ( each time i disconnect all vcenters and connect again to powercli)

Hope I answered your query,.

0 Kudos
thiag2011
Enthusiast
Enthusiast

Any other info required LucD...

please let me know

0 Kudos
LucD
Leadership
Leadership

Can you try this revised version ?

&{foreach($vc in $global:DefaultVIServers){

  $si = Get-View ServiceInstance -Server $vc

  $licMgr = Get-View -Id $si.Content.LicenseManager

  $asgLicMgr = Get-View -Id $licMgr.LicenseAssignmentManager

  foreach($esx in Get-VMHost -Server $vc){

    $licAssigned = $asgLicMgr.QueryAssignedLicenses($esx.ExtensionData.MoRef.Value)

    $esx | Select @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$_.Name.Split('.')[0]}},

            @{N='LicenseKey';E={$licAssigned[0].AssignedLicense.LicenseKey}}

  }

}} | Group-Object -Property LicenseKey | %{

  New-Object PSObject -Property @{

    Key = $_.Name.split('-')[0,4] -join '-xxxxx-xxxxx-xxxxx-'

    vCenters = ($_.Group | Select -ExpandProperty vCenter | Sort-Object -Unique) -join ','

    nrvCenters = ($_.Group | Select -ExpandProperty vCenter | Sort-Object -Unique).Count

  }

} | where{$_.nrvCenters -gt 1}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi LucD,

Thanks,

I tried to execute the new script after connecting to two vcenters, which has same license ( i assigned same license in both vcenters for testing purpose)

But I got result as below.

PowerCLI C:\License Scripts> .\test.ps1

PowerCLI C:\License Scripts>

i.e; I got two empty lines and then again the prompt.

0 Kudos
LucD
Leadership
Leadership

In which vSphere version and with which PowerCLI version are you running this ?

Does this return all the licenses in both vCenters ?

&{foreach($vc in $global:DefaultVIServers){

  $si = Get-View ServiceInstance -Server $vc

  $licMgr = Get-View -Id $si.Content.LicenseManager

  $asgLicMgr = Get-View -Id $licMgr.LicenseAssignmentManager

  foreach($esx in Get-VMHost -Server $vc){

    $licAssigned = $asgLicMgr.QueryAssignedLicenses($esx.ExtensionData.MoRef.Value)

    $esx | Select @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$_.Name.Split('.')[0]}},

            @{N='LicenseKey';E={$licAssigned[0].AssignedLicense.LicenseKey}}

  }

}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi LucD,

Powercli version is as below

PowerCLI Version

----------------

   VMware vSphere PowerCLI 5.1 Release 1 build 793510

---------------

Snapin Versions

---------------

   VMWare AutoDeploy PowerCLI Component 5.1 build 768137

   VMWare ImageBuilder PowerCLI Component 5.1 build 768137

   VMware License PowerCLI Component 5.1 build 669840

   VMware vSphere PowerCLI Component 5.1 build 793489

   VMware vSphere Update Manager PowerCLI 5.1 build 782803

Vsphere version:

Vcenter 5.1 build 1473063

Yes the latest script is getting license from all the vcenters it is connected to...

0 Kudos
LucD
Leadership
Leadership

Then it should work, that API method the script is using was introduced in vSphere 5.0.

If that script returns all the licenses with the correct vCenter, the grouping should work.

Does the following give the correct groups, check the Count property

&{foreach($vc in $global:DefaultVIServers){

  $si = Get-View ServiceInstance -Server $vc

  $licMgr = Get-View -Id $si.Content.LicenseManager

  $asgLicMgr = Get-View -Id $licMgr.LicenseAssignmentManager

  foreach($esx in Get-VMHost -Server $vc){

    $licAssigned = $asgLicMgr.QueryAssignedLicenses($esx.ExtensionData.MoRef.Value)

    $esx | Select @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$_.Name.Split('.')[0]}},

            @{N='LicenseKey';E={$licAssigned[0].AssignedLicense.LicenseKey}}

  }

}} | Group-Object -Property LicenseKey | Format-List


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thiag2011
Enthusiast
Enthusiast

LucD, today I was checking the output of your script which listed all the licenses for the connected vcenters.

I exported that and manually verified to find, there were licenses that are assigned to two different vcenters.

But when I manually verified by logging in the vcenters, I could find no such license is assigned in one of the vcenters.

Eg if I could see license 5J0C0-xxxxx-xxxxx-xxxxx-F8042 assigned to abcd123, bcdh567 on checking the output of the script.

But when I check in vcenter, I see 5J0C0-xxxxx-xxxxx-xxxxx-F8042 is assigned to abcd123 and some other license is assigned in bcdh567.

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi LucD,

Please find below the part of the output of latest script.

The count value increases in two cases

Case 1: License allocated to ESXi host of two different vcenters

Case 2: License allocated to ESXi host of same vcenters ( as license is for 4 cpu and is shared between two number of  2 CPU host)

Case1:

Name   : 0H6CL-xxxxx-xxxxx-xxxxx-21eP4

Count  : 3

Group  : {@{vCenter=vcenter1; VMHost=host1;

         LicenseKey=0H6CL-xxxxx-xxxxx-xxxxx-21eP4},

         @{vCenter=vcenter2; VMHost=host2;

         LicenseKey=0H6CL-xxxxx-xxxxx-xxxxx-21eP4},

         @{vCenter=vcenter2; VMHost=host3;

         LicenseKey=0H6CL-xxxxx-xxxxx-xxxxx-21eP4}}

Values : {0H6CL-xxxxx-xxxxx-xxxxx-21eP4}


CASE 2:

Name   : NM03H-xxxxx-xxxxx-xxxxx-8NLLN

Count  : 2

Group  : {@{vCenter=vcenter2; VMHost=host5;

         LicenseKey=NM03H-xxxxx-xxxxx-xxxxx-8NLLN},

         @{vCenter=vcenter2; VMHost=host7;

         LicenseKey=NM03H-xxxxx-xxxxx-xxxxx-8NLLN}}

Values : {NM03H-xxxxx-xxxxx-xxxxx-8NLLN}

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi LucD,

Hope you had a look at my update on the output I got for both the latest script provided by you.

Any input for me please..

0 Kudos
LucD
Leadership
Leadership

Another version, can you try this one ?

$keys = foreach($vc in $global:DefaultVIServers){

  $si = Get-View ServiceInstance -Server $vc

  $licMgr = Get-View -Id $si.Content.LicenseManager

  $asgLicMgr = Get-View -Id $licMgr.LicenseAssignmentManager

  foreach($esx in Get-VMHost -Server $vc){

    $licAssigned = $asgLicMgr.QueryAssignedLicenses($esx.ExtensionData.MoRef.Value)

    $esx | Select @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$_.Name.Split('.')[0]}},

            @{N='LicenseKey';E={$licAssigned[0].AssignedLicense.LicenseKey}}

  }

}

$keys | Group-Object -Property LicenseKey | %{

  $_.Group | Sort-Object -Property vCenter -Unique |

  Group-Object -Property vCenter |

  where{$_.Count -gt 1} |

  Select @{N='Licensekey';E={$_.Group[0].LicenseKey}},

    @{N='Count';E={$_.Count}},

    @{N='vCenter';E={[string]::Join(',',($_.Group | Select -ExpandProperty vCenter))}}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos