Create an array of VMs and sort on OMStat Key value fails. Goal is highest Value at top of array/list of VMs.
[array]($vms2rs = Get-VM -Tag yes | where{(Get-VM -Tag test).Name -Contains $_.Name}).Name
ForEach ($item in $vms2rs) {
$Name = $item.Name
[array]($deltascore = Get-VM -Name $Name | Get-OMResource |
Get-OMStat -Key "Super Metric|sm_6dcf4b1d-fa56-4119-bef8-b197df54b70a" -From ([DateTime]::Now).AddMinutes(-120) |
Select-Object Resource, Value -Last 1 | Sort-Object Value)
}
Output:
rhel79-test
2016test
tstviewcs
tstviewss
Resource : rhel79-test
Value : 2
Resource : 2016test
Value : 5
Resource : tstviewcs
Value : 6
Resource : tstviewss
Value : 3
I think there was a mistake in that code, try like this
$data = ForEach ($item in $vms2rs) {
$Name = $item.Name
Get-VM -Name $Name | Get-OMResource |
Get-OMStat -Key "Super Metric|sm_6dcf4b1d-fa56-4119-bef8-b197df54b70a" -From ([DateTime]::Now).AddMinutes(-120) |
Select-Object @{N='VM';E={$name}},Resource, Value -Last 1
}
$data | Sort-Object -Property Value | select -Property VM
Normally the one who asks the question marks the correct answer.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You only take the last sample returned by Get-OMStat.
With the -Last 1 the Sort-Object doesn't really make any sense
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I only want to know the latest, real-time value of the supermetric, and sort on that. Removing the -Last 1 from the Select-Object returns all values of the supermetric from vrops.
***-rhel79-test
*****2016test
***tstviewcs
***tstviewss
Resource : ***-rhel79-test
Value : 2
Resource : ***-rhel79-test
Value : 2
Resource : ***-rhel79-test
Value : 2
Resource : ***-rhel79-test
Value : 2
Resource : ***-rhel79-test
Value : 2
Resource : ***-rhel79-test
Value : 2
Resource : ***-rhel79-test
Value : 2
Resource : ***-rhel79-test
Value : 2
Ad Infinitum...
But if you want the highest value you should first do the sort, and then select the last one.
Now you are only providing 1 object to the sort.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I probably didn't explain the desired result well enough: I'm attempting to get a list of VMs from vCenter, get the latest value of a supermetric for each VM from vROps, and sort the list of VMs based on that latest supermetric value. For example, I get VMx, VMy, and VMz from vCenter, I get a supermetric value of 2 for VMx, 7 for VMy, and 5 for VMz. I want to make an array or a list of the VMs with VMy first, VMz second, and VMx third.
VMy 7
VMz 5
VMx 2
Make sense? I'm probably doing it the wrong way, so I'm happy to take suggestions.
I don't want to sort on the range of supermetric values that vrops returms for one VM and pick the highest one. I want to take the latest supermetric value from vrops and sort my list of VMs on that..
In that case your sort is still out of place.
You will have to sort on the value after the foreach loop on all returned objects
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok, sort after the for loop:
ForEach ($item in $vms2rs) {
$Name = $item.Name
[array]($deltascore = Get-VM -Name $Name | Get-OMResource |
Get-OMStat -Key "Super Metric|sm_6dcf4b1d-fa56-4119-bef8-b197df54b70a" -From ([DateTime]::Now).AddMinutes(-120) |
Select-Object Resource, Value -Last 1)
}
Sort-Object -Property Value
Returns:
oit-rhel79-test
2016test
oittstviewcs
oittstviewss
Resource : rhel79-test
Value : 2
Resource : 2016test
Value : 5
Resource : tstviewcs
Value : 6
Resource : tstviewss
Value : 3
Desired output would be
6
5
3
2
More specifically:
tstviewcs
2016test
tstviewss
rhel79-test
In that order
Since foreach doesn't place anything in the pipeline, you didn't sort anything.
Try something like this
$data = ForEach ($item in $vms2rs) {
$Name = $item.Name
[array]($deltascore = Get-VM -Name $Name | Get-OMResource |
Get-OMStat -Key "Super Metric|sm_6dcf4b1d-fa56-4119-bef8-b197df54b70a" -From ([DateTime]::Now).AddMinutes(-120) |
Select-Object Resource, Value -Last 1)
}
$data | Sort-Object -Property Value
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, that works, and thank you:
rhel79-test
2016test
tstviewcs
tstviewss
Resource : rhel79-test
Value : 2
Resource : tstviewss
Value : 3
Resource : 2016test
Value : 5
Resource : tstviewcs
Value : 6
Two questions: How can we return just the VM names? And more generally, when posting a new question to the board, should I post only my question like "How do I Get VMs from vCenter and sort on vROps supermetrics"? Or should I post my attempt at the script along with what I am solving for like I did in this discussion?
Thanks again.
Something like this
$data = ForEach ($item in $vms2rs) {
$Name = $item.Name
[array]($deltascore = Get-VM -Name $Name | Get-OMResource |
Get-OMStat -Key "Super Metric|sm_6dcf4b1d-fa56-4119-bef8-b197df54b70a" -From ([DateTime]::Now).AddMinutes(-120) |
Select-Object @{N='VM';E={$name}},Resource, Value -Last 1)
}
$data | Sort-Object -Property Value | select -Property VM
I always prefer when the one asking a question has at least tried to find a solution.
Just posting a question for a script makes this community look too much like a scripting service.
And I will, most of the time, reply with "What have you already tried?"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$data | Sort-Object -Property Value | select -Property VM
returned:
VM :
VM :
VM :
VM :
I tried $data | Sort-Object -Property Value -Descending | Select Resource
that returned:
Resource : tstviewcs
Resource : 2016test
Resource : tstviewss
Resource : rhel79-test
Now if I could just get the VM names without "Resource :"
And last question: do I mark responses with "correct answer" or do you?
Thanks
I think there was a mistake in that code, try like this
$data = ForEach ($item in $vms2rs) {
$Name = $item.Name
Get-VM -Name $Name | Get-OMResource |
Get-OMStat -Key "Super Metric|sm_6dcf4b1d-fa56-4119-bef8-b197df54b70a" -From ([DateTime]::Now).AddMinutes(-120) |
Select-Object @{N='VM';E={$name}},Resource, Value -Last 1
}
$data | Sort-Object -Property Value | select -Property VM
Normally the one who asks the question marks the correct answer.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you. Much appreciated.