VMware Cloud Community
cgarciamoran
Contributor
Contributor
Jump to solution

Getting a Value from a Hash

Folks;

Im trying to get a value from a Hash that matches another value in a larger ForEach Loop, but the way I am doing it either gets me blanks or the entire hash line that matches my select object. Any ideas on where im not doing right. Im am creating an host / vm inventory and already had most of the info done already but thought to add License Name and Key which is proving to be troublesome.

thanks!

Here's what I have and the ways ive tried to do it, the hash works fine to get the licensing when I look at all the values from $licensing.

$licensing = foreach ($vCenter in $VC)`

{

$licMgr = Get-View LicenseManager -Server $vCenter

$licAssignmentMgr = Get-View -Id $licMgr.LicenseAssignmentManager -Server $vCenter

$licAssignmentMgr.QueryAssignedLicenses($vCenter.InstanceUid) | % `

{

$_ | select `

EntityDisplayName,`

@{N='LicenseKey';E={$_.AssignedLIcense.LicenseKey}},`

@{N='LicenseName';E={$_.AssignedLicense.Name}}

}

}

## Example #1

$lic = $licensing | where-object { $_.EntityDisplayName -like $vmhost } | select-object {$_.AssignedLicense.'Name'}

$key = $licensing | where-object { $_.EntityDisplayName -like $vmhost } | select-object {$_.AssignedLIcense.'LicenseKey'}

## Example #2

$lic = $licensing.Keys | ? { $licensing[$_.EntityDisplayName] -eq $vmhost  } | select-object $_.AssignedLicense.Name

$key = $licensing.Keys | ? { $licensing[$_.EntityDisplayName] -eq $vmhost  } | select-object $_.AssignedLIcense.LicenseKey

## Example #3

$lic = $licensing  |  foreach-object $_.AssignedLicense.Name | where-object { $_.EntityDisplayName -like $vmhost }

$key = $licensing  |  foreach-object $_.AssignedLIcense.LicenseKey | where-object { $_.EntityDisplayName -like $vmhost }

## Example #4

$lic = $licensing | where-object { $_.EntityDisplayName -like $vmhost } | foreach-object {$_.AssignedLicense.'Name'}

$key = $licensing | where-object { $_.EntityDisplayName -like $vmhost } | foreach-object {$_.AssignedLicense.'LicenseKey'}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Provided I got the question correctly, you should do

$lic = $licensing | where-object { $_.EntityDisplayName -like $vmhost.Name } | select-object -ExpandProperty LicenseName

$key = $licensing | where-object { $_.EntityDisplayName -like $vmhost.Name } | select-object -ExpandProperty LicenseKey


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Provided I got the question correctly, you should do

$lic = $licensing | where-object { $_.EntityDisplayName -like $vmhost.Name } | select-object -ExpandProperty LicenseName

$key = $licensing | where-object { $_.EntityDisplayName -like $vmhost.Name } | select-object -ExpandProperty LicenseKey


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

Reply
0 Kudos
cgarciamoran
Contributor
Contributor
Jump to solution

Perfect thanks, didnt think to do expandproperty! Worked like a peach!

Reply
0 Kudos
cgarciamoran
Contributor
Contributor
Jump to solution

Oddly enough some of the 5.5 vcenters return no data when getting queried for licensing,has anyone ever seen this? Theyre all Windows Servers and have been rebooted some time ago, other pwoercli commands work fine on them

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid not.
On the other hand, I can't test since I don't have any vCenter 5.5 on Windows anymore.


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

Reply
0 Kudos
cgarciamoran
Contributor
Contributor
Jump to solution

Thanks I'll keep poking around we jsut patched one of them to the latest 5.5 patches and still get blanks.

I know I can do this to expose the license key and that works fine on 5.5 / 6.5 , is the License type expose there too? I didn't seem to find that either

Get-VMHost -Name ESX1 | Select-Object -Property LicenseKey

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have an account to connect to the ESXi nodes, you could do something like this.
You need to be connected to the VCSA to start with.

$user = 'root'

$pswd = 'VMware1!'


Get-VMHost -PipelineVariable esx |

ForEach-Object -Process {

   Connect-VIServer -Server $esx.Name -User $user -Password $pswd | Out-Null

   $si = Get-View ServiceInstance -Server $esx.Name

   $licMgr = Get-View -Id $si.Content.LicenseManager -Server $esx.Name

   $licMgr.Licenses | Select @{N = 'VMHost'; E = { $esx.Name } },Name, LicenseKey

   Disconnect-VIServer -Server $esx.Name -Confirm:$false

}


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

Reply
0 Kudos