June 17, 2020 at 10:32 am #236167ParticipantTopics: 2Replies: 3Points: 34Rank: Member
An interesting situation I ran into.
Working with VMware PowerCLI to gather performance metrics from vCenter. One of the checks I need to do is to find out the statistics collection level. That returns an array and part of which looks like below. As you may notice that it has a property called “Length” (that should have been named “Duration” but that is beside the point 🙂 ) and I need to use the values contained in it.
PS C:\Temp> $perfMgr.HistoricalInterval | ft Key SamplingPeriod Name Length Level Enabled --- -------------- ---- ------ ----- ------- 1 300 Past day 86400 1 True 2 1800 Past week 604800 1 True 3 7200 Past month 2592000 1 True 4 86400 Past year 31536000 1 True
I am trying to get to the values contained within the property Length (eg: 86400). Since it shares a name with the property that gives me how big the Array is (in this case 4) I always get that instead of the value in the property Length. Please see below.
PS C:\Temp> $perfMgr.HistoricalInterval.SamplingPeriod 300 PS C:\Temp> $perfMgr.HistoricalInterval.Name Past day PS C:\Temp> $perfMgr.HistoricalInterval.Length 4 PS C:\Temp>
How do I get the value I am looking for?
Thanks in advance.
June 17, 2020 at 11:01 am #236185ParticipantTopics: 12Replies: 1643Points: 2,665Rank: Community Hero
The array index is in the wrong place. You are getting the object length’s first item. The reason it’s working for your other is Powershell is returning an array containing the ‘SamplingPeriod’ and you are getting the first item in the array.
$temp = @" Key,SamplingPeriod,Name,Length,Level,Enabled 1,300,Past day,86400,1,True 2,1800,Past week,604800,1,True 3,7200,Past month,2592000,1,True 4,86400,Past year,31536000,1,True "@ | ConvertFrom-Csv $temp $temp.Length $temp | Select -ExpandProperty Length
PS C:\Users\rasim> $temp.Length 86400 PS C:\Users\rasim> $temp | Select -ExpandProperty Length 86400 PS C:\Users\rasim> $temp.SamplingPeriod 300
June 17, 2020 at 11:10 am #236188ParticipantTopics: 2Replies: 3Points: 34Rank: Member
You are my Hero Rob !!
- You must be logged in to reply to this topic.