Home > Blogs > VMware PowerCLI Blog


Retrieving License keys from Multiple vCenters

AlanFeb2012_thumb_thumb1_thumb_thumb[1]
Posted by
Alan Renouf
Technical Marketing

A question I receive all the time is:

How can I list all license keys I have for all of my Virtual Centers in my Infrastructure ?

 

Luckily PowerCLI enables us to work with multiple vCenters all at one, we can connect to many vCenters or hosts and pull back information on all of these at the same time, to do this you need to set the PowerCLI Configuration to work in multiple mode and then you can connect to more than one vCenter at the same time, to do this see the following example:

SNAGHTMLc8e21f3

Once we are in multiple configuration mode we can then use Connect-VIserver on as many vCenters or hosts as you like, we can use this to connect to multiple vCenters with the same credentials or just list each one like below:

Connect to multiple vCenters with the same username and password:

Connect-VIServer VC1, VC2, VC3, VC4 –User Administrator –Password MyPa$$word

Connect to multiple vCenters with different credentials:

Connect-VIServer VC1 –User Administrator1 –Password MyPa$$word1

Connect-VIServer  VC2 –User Administrator2 –Password MyPa$$word2

Connect-VIServer VC3 –User Administrator3 –Password MyPa$$word3

Connect-VIServer VC4 –User Administrator4 –Password MyPa$$word4

Once connected this information is stored in the $DefaultVIServers variable, this can easily be viewed at any time:

SNAGHTMLc96fa66

We are also easily able to run scripts against these connections, like for example the License script at the end of this post, this lists all license keys and their information for each vCenter:

(You will excuse me if I exclude the actual key)

SNAGHTMLcc99b96

The Code

# Set to multiple VC Mode
if(((Get-PowerCLIConfiguration).DefaultVIServerMode) -ne "Multiple") {
    Set-PowerCLIConfiguration -DefaultVIServerMode Multiple | Out-Null
}

# Make sure you connect to your VCs here

# Get the license info from each VC in turn
$vSphereLicInfo = @()
$ServiceInstance = Get-View ServiceInstance
Foreach ($LicenseMan in Get-View ($ServiceInstance | Select -First 1).Content.LicenseManager) {
    Foreach ($License in ($LicenseMan | Select -ExpandProperty Licenses)) {
        $Details = "" |Select VC, Name, Key, Total, Used, ExpirationDate , Information
        $Details.VC = ([Uri]$LicenseMan.Client.ServiceUrl).Host
        $Details.Name= $License.Name
        $Details.Key= $License.LicenseKey
        $Details.Total= $License.Total
        $Details.Used= $License.Used
        $Details.Information= $License.Labels | Select -expand Value
        $Details.ExpirationDate = $License.Properties | Where { $_.key -eq "expirationDate" } | Select -ExpandProperty Value
        $vSphereLicInfo += $Details
    }
}
$vSphereLicInfo | Format-Table -AutoSize

 

Get notification of new blog postings and more by following VMware PowerCLI on Twitter: @PowerCLI

This entry was posted in Uncategorized on by .
Alan Renouf

About Alan Renouf

Alan Renouf is a Product Line Manager at VMware focusing on API's, SDK's and CLI's, He is responsible for providing the architects and operators of private and public cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/PowerCLI a book author and has a personal blog at http://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

10 thoughts on “Retrieving License keys from Multiple vCenters

  1. Michael

    Thanks, Alan!
    Is there a way to exclude licenses by name?
    ex: ” Product Evaluation”

    Reply
  2. Alan Renouf

    Sure is Michael, you can use the below as an example:
    # Set to multiple VC Mode
    if(((Get-PowerCLIConfiguration).DefaultVIServerMode) -ne “Multiple”) {
    Set-PowerCLIConfiguration -DefaultVIServerMode Multiple | Out-Null
    }
    # Make sure you connect to your VCs here
    # Get the license info from each VC in turn
    $vSphereLicInfo = @()
    $ServiceInstance = Get-View ServiceInstance
    Foreach ($LicenseMan in Get-View ($ServiceInstance | Select -First 1).Content.LicenseManager) {
    Foreach ($License in ($LicenseMan | Select -ExpandProperty Licenses | Where {$_.Name -ne “Product Evaluation”} )) {
    $Details = “” |Select VC, Name, Key, Total, Used, ExpirationDate , Information
    $Details.VC = ([Uri]$LicenseMan.Client.ServiceUrl).Host
    $Details.Name= $License.Name
    $Details.Key= $License.LicenseKey
    $Details.Total= $License.Total
    $Details.Used= $License.Used
    $Details.Information= $License.Labels | Select -expand Value
    $Details.ExpirationDate = $License.Properties | Where { $_.key -eq “expirationDate” } | Select -ExpandProperty Value
    $vSphereLicInfo += $Details
    }
    }
    $vSphereLicInfo | Format-Table -AutoSize

    Reply
  3. Michael

    Awesome, this is going to be extremely handy for our environment.
    Thanks, again!

    Reply
  4. Jim

    I am having trouble understanding these 3 lines of code. I appreciate a clear/simple translation of what each line is doing:
    $ServiceInstance = Get-View ServiceInstance
    Foreach ($LicenseMan in Get-View ($ServiceInstance | Select -First 1).Content.LicenseManager) {
    Foreach ($License in ($LicenseMan | Select -ExpandProperty Licenses))

    Reply
  5. Vitor

    Great Script, hardly to find some good script like this, I surrounded the whole internet to find one like this to help with licenses!! Thanks!

    Reply
  6. Aria

    Great script, I am trying to add a column for what esx hosts each key assigned.

    VC Name Key Total Used ExpirationDate Information Host
    — —- — —– —- ————– ———– —-
    vcs01 Product Evaluation 00000-00000-00000-00000-00000 0
    vcs01 VMware v 6 Standard M572N-2CJBP-N8K9D-0K4HH-1P0MZ 1 1

    I can do that with the following script: Part “$detail.Host = $license.EntityDisplayName”, but I am trying to add like this to your script, I couldn’t find what I have to use .

    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
    }

    Reply
    1. redtechnet

      Did you ever get this figured out?

      Reply
  7. Steve Roach

    How would I export the results to csv? I’m sorry, I’m fairly new to powershell.

    Reply
    1. Steve Roach

      Never mind, I figured it out, thanks!!

      Reply
  8. Mehdi

    Hello Alan,

    I used your script to display informations on Licensing but in the colunm “Expiration Date” the date of expiration of licensing not display.

    Do you have a idea regarding this issue?

    Thank you very much

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*