How to get a value from a property named Length instead of the size of the Array

Welcome Forums General PowerShell Q&A How to get a value from a property named Length instead of the size of the Array

Viewing 2 reply threads
  • Author
    Posts
    • #236167
      Participant
      Topics: 2
      Replies: 3
      Points: 34
      Rank: 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[0] 
      300
      PS C:\Temp> $perfMgr.HistoricalInterval.Name[0] 
      Past day
      PS C:\Temp> $perfMgr.HistoricalInterval.Length[0] 
      4
      PS C:\Temp>

      How do I get the value I am looking for?

      Thanks in advance.

    • #236185
      Participant
      Topics: 12
      Replies: 1643
      Points: 2,665
      Helping Hand
      Rank: 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[0].Length
      $temp[0] | Select -ExpandProperty Length
      

      Output:

      PS C:\Users\rasim> $temp[0].Length
      
      86400
      
      PS C:\Users\rasim> $temp[0] | Select -ExpandProperty Length
      86400
      
      PS C:\Users\rasim> $temp[0].SamplingPeriod
      300
      
    • #236188
      Participant
      Topics: 2
      Replies: 3
      Points: 34
      Rank: Member

      You are my Hero Rob !!

      Thank you!

Viewing 2 reply threads
  • You must be logged in to reply to this topic.