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'}
Provided I got the question correctly, you should do
$key = $licensing | where-object { $_.EntityDisplayName -like $vmhost.Name } | select-object -ExpandProperty LicenseKey
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Provided I got the question correctly, you should do
$key = $licensing | where-object { $_.EntityDisplayName -like $vmhost.Name } | select-object -ExpandProperty LicenseKey
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect thanks, didnt think to do expandproperty! Worked like a peach!
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
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
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
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.
$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