Automation

 View Only
  • 1.  Get hosts using vSphere licenses

    Posted Apr 04, 2019 05:26 PM

    Hi,

    I have the code below which tells me the vCenter, the name of the key, the license key, the total number of licenses per key and the total used. However, I'd also like to see which hosts are using each key. Is there a way to achieve this in one script? My code is below, which I actually got from another forum (or perhaps this one)

    $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

            $Details.VC = ([Uri]$LicenseMan.Client.ServiceUrl).Host

            $Details.Name= $License.Name

            $Details.Key= $License.LicenseKey

            $Details.Total= $License.Total

            $Details.Used= $License.Used

            $vSphereLicInfo += $Details

         }

    }

    $vSphereLicInfo | Export-Csv $PSScriptRoot\report.csv -UseCulture



  • 2.  RE: Get hosts using vSphere licenses
    Best Answer

    Posted Apr 04, 2019 05:38 PM

    Try something like this

    $vSphereLicInfo = @()

    $ServiceInstance = Get-View ServiceInstance

    $lictab = @{}

    Get-VMHost | Group-Object -Property LicenseKey |

    ForEach-Object -Process {

       $lictab.Add($_.Name,$_.Group.Name -join '|')

    }


    Foreach ($LicenseMan in Get-View ($ServiceInstance | Select -First 1).Content.LicenseManager) {

       Foreach ($License in ($LicenseMan | Select -ExpandProperty Licenses)) {

       $Details = "" |Select VC, Name, VMHost, Key, Total, Used

       $Details.VC = ([Uri]$LicenseMan.Client.ServiceUrl).Host

       $Details.Name= $License.Name

       $Details.Key= $License.LicenseKey

       $Details.VMHost = $licTab.Item($License.LicenseKey)

       $Details.Total= $License.Total

       $Details.Used= $License.Used

       $vSphereLicInfo += $Details

       }

    }

    $vSphereLicInfo | Export-Csv $PSScriptRoot\report.csv -UseCulture



  • 3.  RE: Get hosts using vSphere licenses

    Posted Apr 04, 2019 05:56 PM

    Man you're good. Thank you sir! You've made my engineers very happy!