PowerCLI

 View Only
Expand all | Collapse all

Audit Licenses

LucD

LucDDec 03, 2022 08:58 PM

  • 1.  Audit Licenses

    Posted Jul 18, 2022 09:01 PM

    I am trying to get license details from my vCenter, specifically when they expire.  I can get the edition, total used, etc., however I cannot seem to find the property/key for the license expire value.  Here is what I have so far-

     

    ForEach ($Licenses in (Get-View $Global:DefaultVIServer.ExtensionData.Content.LicenseManager).Licenses){
     
    ForEach ($Property in $Licenses){

    $licenseObj = New-Object PSObject

    $licenseObj | Add-Member -Name "vCenter Name" -MemberType NoteProperty -Value $Global:DefaultVIServer.Name
    $licenseObj | Add-Member -Name "License Name" -MemberType NoteProperty -Value $Property.Name
    $licenseObj | Add-Member -Name "License Key" -MemberType NoteProperty -Value $Property.LicenseKey
    #$licenseObj | Add-Member -Name "Expiration Date" -MemberType NoteProperty -Value $Property.?????

    $licenseObj
    }
    }
     
     
    Commented out the line I am missing


  • 2.  RE: Audit Licenses



  • 3.  RE: Audit Licenses

    Posted Jul 18, 2022 09:10 PM

    Thank you sir- appreciate the help!

    I am pulling this detail from vCenter, about each license that is installed.  Not the individual hosts but rather vCenter version/license key, vSAN version/license key, etc. for each product registered in vCenter.  The column I can't get details on is the Expired check box.  I know there are some vCenter's that have a 60 day or 90 key or even one that expires every year!  Is there a way to expose this value?

    Thanks!



  • 4.  RE: Audit Licenses
    Best Answer

    Posted Jul 18, 2022 10:06 PM

    Ok, then try like this

    $LicenseManager= Get-view LicenseManager
    $LicenseManager.Licenses | where{$_.Total -ne 0} |
    Select Name,LicenseKey,Used,Total,
      @{N='ExpirationDate';E={$_.Properties | where{$_.Key -eq 'expirationDate'} | Select -ExpandProperty Value}}


  • 5.  RE: Audit Licenses

    Posted Dec 03, 2022 05:42 PM

    Hi

    Can anyone help me on this script to include license remaining days to expire in the below script, this is bit urgent.

    ForEach ($vc in $vcservers){
    Connect-VIserver -Server $vc -User $user -Password $password
    $licenseManager = Get-View $Global:DefaultVIServers.ExtensionData.content.LicenseManager
    $licenseAssignmentManager = Get-View $LicenseManager.licenseAssignmentManager
    $assignedLicenses = $licenseAssignmentManager.QueryAssignedLicenses($global.defaultviservers.instanceuuid) | Group-Object -Property EntityDisplayName

    foreach ($license in $assignedLicenses)
    {
    #$vCenterName = ([System.uri]$license.Client.ServiceUrl).Host
    $lic = $license.Group[0]
    $licenseObj = [PSCustomObject]@{
    vcenter = $vc
    EntityDisplayName = $lic.EntityDisplayName
    Name = $lic.AssignedLicense.Name
    LicenseKey = $lic.AssignedLicense.LicenseKey
    EditionKey = $lic.AssignedLicense.EditionKey
    ProductName = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'ProductName'} | Select-Object -ExpandProperty Value

    ProductVersion = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'ProductVersion'} | Select-Object -ExpandProperty Value
    ExpirationDate = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'ExpirationDate'} | Select-Object -ExpandProperty Value
    ExpirationHours = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'expirationHours'} | Select-Object -ExpandProperty Value
    ExpirationMins= $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'expirationMinutes'} | Select-Object -ExpandProperty Value

    }
    $licenseObj
    $licenseUsage += $licenseObj
    }
    }



  • 6.  RE: Audit Licenses

    Posted Dec 03, 2022 05:59 PM

    Add this property

                DaysToExpiration = [math]::Round((New-TimeSpan -End ($lic.AssignedLicense.Properties | Where-Object { $_.Key -eq 'ExpirationDate' }).Value -Start $now).TotalDays,0)
    


  • 7.  RE: Audit Licenses

    Posted Dec 03, 2022 06:18 PM

    Hi Luc

    I get this error msg as below when i execute

    New-TimeSpan : Cannot bind parameter 'End' to the target. Exception setting "End": "Cannot convert null to type "System.DateTime"."
    At S:\srini\scripts\licdetailextract_final.ps1:29 char:61
    + ... meSpan -End ($lic.AssignedLicense.Properties | Where-Object { $_.Key ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : WriteError: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand



  • 8.  RE: Audit Licenses

    Posted Dec 03, 2022 06:37 PM

    That means there are entries where there is no expiration date



  • 9.  RE: Audit Licenses

    Posted Dec 03, 2022 06:54 PM
      |   view attached

    Hi LucD

    Thanks for your response, I really appreciate it.

    Please find the enclosed script output in this post for your references, there are no values in expiration date except few.

    Attachment(s)

    xlsx
    scriptoutput.xlsx   10 KB 1 version


  • 10.  RE: Audit Licenses

    Posted Dec 03, 2022 07:59 PM

    To cope with missing values, you could do

                DaysToExpiration = &{
                    $daysExp = $lic.AssignedLicense.Properties | Where-Object { $_.Key -eq 'ExpirationDate' }
                    if($daysExp -ne $null){
                        [math]::Round((New-TimeSpan -End $daysExp.Value -Start $now).TotalDays,0)
                    }
                    else{'na'}
                }
    


  • 11.  RE: Audit Licenses

    Posted Dec 03, 2022 08:33 PM

    Nope, it didnt work, below is the error message:

    Where-Object : A positional parameter cannot be found that accepts argument 'if'.
    At line:30 char:76
    + ... roperties | Where-Object { $_.Key -eq 'ExpirationDate' } if($daysExp ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand



  • 12.  RE: Audit Licenses

    Posted Dec 03, 2022 08:43 PM

    Looks like you are missing some CR-LF in the copy-paste.
    That If should start on a new line



  • 13.  RE: Audit Licenses

    Posted Dec 03, 2022 08:58 PM

    It should look like this
    lines.jpg



  • 14.  RE: Audit Licenses

    Posted Dec 04, 2022 04:42 AM

    Thanks a lot LucD, it worked, I was pasting to single line.

    Thanks again for all your help.



  • 15.  RE: Audit Licenses

    Posted Dec 04, 2022 07:26 PM

     

     



  • 16.  RE: Audit Licenses

    Posted Dec 05, 2022 12:00 PM

    Hi Luc

    For some reason, Iam seeing 2 entries for each line, can you check on the script and let me know if you see any findings.

    Srinivas



  • 17.  RE: Audit Licenses

    Posted Dec 05, 2022 12:17 PM

    You are importing the same file twice



  • 18.  RE: Audit Licenses

    Posted Dec 05, 2022 12:24 PM

    Luc, Can you fix the script, I couldnt find where its importing twice.



  • 19.  RE: Audit Licenses

    Posted Dec 05, 2022 12:28 PM

    You are importing the same CSV in $html and $html2



  • 20.  RE: Audit Licenses

    Posted Dec 05, 2022 12:55 PM

    Hi,

    Before html conversion also ie in raw output Iam seeing double entries.



  • 21.  RE: Audit Licenses

    Posted Dec 05, 2022 12:58 PM

    I get the error msg as below:

    Connect-VIServer : Cannot validate argument on parameter 'Server'. The argument is null or empty. Provide an argument that is not null or empty, and then
    try the command again.
    At line:5 char:26
    + Connect-VIserver -Server $vcservers -User $user -Password $password
    + ~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Connect-VIServer], ParameterBindingValidationException



  • 22.  RE: Audit Licenses

    Posted Dec 05, 2022 01:25 PM

    Your script says

    Connect-VIserver -Server $vc -User $user -Password $password

    Why do you use $vcservers now?



  • 23.  RE: Audit Licenses

    Posted Dec 05, 2022 04:07 PM

    Hello Luc

    Iam seeing couple of issues in the outcome of this script, few values doesnt corelate with actual data, specifically on license expiration date and entity display name for few line items.

    vcenter actual expiration date for vcenter vc01 and vc02 is 01/02/2023 but in script output it is showing as 01/03/2023...not sure is there any mis calculation or data fetching is wrong here.

    I have enclosed output of this script for your reference, can you please take a look and help to get the issue fixed at the earliest.

    Regards

    Srinivas

     



  • 24.  RE: Audit Licenses

    Posted Dec 05, 2022 06:42 PM

    Hope you understood the issue, let me know if not, will try to explain in detail.



  • 25.  RE: Audit Licenses

    Posted Dec 06, 2022 12:13 PM

    Hello Luc

    Any update on this.



  • 26.  RE: Audit Licenses

    Posted Dec 07, 2022 07:22 AM

    Hi Luc 

    Issue is resolved after removing loop for each vcenter. Thanks for your support as always.

     



  • 27.  RE: Audit Licenses

    Posted Dec 07, 2022 01:02 PM

    Luc, can you help me to get vcenter name for each line item.


    foreach ($license in $assignedLicenses)
    {

    $lic = $license.Group[0]
    $licenseObj = [PSCustomObject]@{
    vcenter = $licenseManager | Where-Object {$_.Key -eq 'Licensededition'} | Select-Object -ExpandProperty Value
    EntityDisplayName = $lic.EntityDisplayName
    Name = $lic.AssignedLicense.Name
    LicenseKey = $lic.AssignedLicense.LicenseKey
    EditionKey = $lic.AssignedLicense.EditionKey
    ProductName = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'ProductName'} | Select-Object -ExpandProperty Value

    ProductVersion = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'ProductVersion'} | Select-Object -ExpandProperty Value
    ExpirationDate = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'ExpirationDate'} | Select-Object -ExpandProperty Value
    ExpirationHours = $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'expirationHours'} | Select-Object -ExpandProperty Value
    ExpirationMins= $lic.AssignedLicense.Properties | Where-Object {$_.Key -eq 'expirationMinutes'} | Select-Object -ExpandProperty Value
    DaysToExpiration = &{
    $daysExp = $lic.AssignedLicense.Properties | Where-Object { $_.Key -eq 'ExpirationDate' }
    if($daysExp -ne $null){
    [math]::Round((New-TimeSpan -End $daysExp.Value -Start $date).TotalDays,0)
    }
    else{'na'}
    }

    }
    $licenseObj
    $licenseUsage += $licenseObj
    }
    #}
    Disconnect-viserver -Server * -Force –Confirm:$false



  • 28.  RE: Audit Licenses

    Posted Dec 26, 2022 07:14 PM

    Hi Luc

    Can you help with to fix this below script to generate a html output if there is a table content else write "no license due to expire"

    $date = GET-DATE
    $htmlformat = '<title>Lic_info</title>'
    $htmlformat += '<H1>VMware license forecast notification</H1>'
    $htmlformat += '<p>The following report was generated on </p>' + "$date"
    $htmlformat += '<style type="text/css">'
    $htmlformat += 'BODY{background-color:#E5E8E8 ;color:#000000;font-family:Arial Narrow,sans-serif;font-size:12px;}'
    $htmlformat += 'TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;}'
    $htmlformat += 'TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#FCF3CF}'
    $htmlformat += 'TD{border-width: 1px;padding: 6px;border-style: solid;border-color: black;background-color:#CACFD2}'
    $htmlformat += '</style>'
    $bodyformat = '<h1> VMware Environment </h1>'

     

    $html = Import-Csv -delimiter ',' -Path "F:\\licdetails.txt" | Select EntityDisplayName,Name,LicenseKey,EditionKey,ProductName,ProductVersion,ExpirationDate,DaysToExpiration | Sort-Object DaysToExpiration | where-object {($_.DaysToExpiration -ne "na") } |

    if($_.DaysToExpiration -match "^\d+$")

    convertTo-Html -Head $htmlformat -Body {} else {Write-Host "<h1>No licenses due to expire</h1>"}

     

    $html | Out-File F:\VMware_Environment_license_notification.html

    Invoke-Expression F:\VMware_Environment_license_notification.html



  • 29.  RE: Audit Licenses

    Posted Dec 05, 2022 01:23 PM

    Then you have probably more than 1 connection open.
    Check what is in $global:defaultVIServers