none
unable to extract particular element from object[] RRS feed

  • Question

  • I have output from $a as 

    $a
    
    Name  Collage Marks Address Date
    ----  ------- ----- ------- -----
    Andra   JKI     78.9  RRRR    5/15/2020 3:10 PM
    Prakash TYU     67.9  EWSR    6/15/2019 6:10 PM
    Amit    ERT     88.9  RRFR    6/15/2020 7:10 AM
    Nobel   VFR     33.9  TTT     2/15/2020 3:10 PM
    
    $a.gettype()
    IsPublic ISSerial Name       BaseType
    ------- -------- ------      --------
    True     True    Object[]    System.Array
    
    I am unable to exract name or line Amit with Marks 88.9
    due to Object[] no success 
    
    I have tried all like  
    $a=$a | ? {$_.Name -ilike "Amit"} OR contains  

    I convert $a to string or to convertto-json but the json is weared and have no value of any of the fields 

    also tried echo ($a | select -ExpandProperty "Name") OR $a."Marks" OR $a.PSObject.Properties |  ? isGettable| select-object -Property Name but no names are sorted 

    jeudi 18 juin 2020 14:21

Toutes les réponses

  • If the variable $a is an array of objects it won't have any named properties. The elements of the array might have, though.

    If you do "$a[0].gettype()" what is the type that's returned?

    $a = @(
        [PSCustomObject]@{name='Andra';college='JKI';Marks=78.9;Address='RRRR';Date=(Get-Date '5/15/2020 3:10 PM')},
        [PSCustomObject]@{name='Prakash';college='TYU';Marks=67.9;Address='EWSR';Date=(Get-Date '6/15/2019 6:10 PM')},
        [PSCustomObject]@{name='Amit';college='ERT';Marks=88.9;Address='RRFR';Date=(Get-Date '6/15/2020 7:10 AM')},
        [PSCustomObject]@{name='Nobel';college='VFR';Marks=33.9;Address='TTT';Date=(Get-Date '2/15/2020 3:10 PM')}
    )
    $a |  Where-Object name -like "Amit*"
    
    $a.gettype()

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array

    $a[0].gettype()
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    PSCustomObject                           System.Object



    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    jeudi 18 juin 2020 14:43
  • Also, if you use the operator "-like" you have to use a wildcard pattern.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    jeudi 18 juin 2020 14:45
  • $a generated dynamically how I can extract individual fields in it using psobject above also $a[0].gettype() is return Object[]  only, please tell me can I convert to string or json so that I can get element by name also I have used wildcard for name and done all trials with it
    vendredi 19 juin 2020 18:31
  • It's possible that you have an array of arrays in the variable $a.

    How is the content of $a created? Saying it's created "dynamically" provides almost no information. Is the value of $a set by a cmdlet? Or is it set by code in another part of your script?

    Showing the code that manages the loading of $a will make your problem a lot clearer.

    Extracting data from a PSCustomObject isn't your problem, unless that's the data type you're dealing with. I used that as an example because I have no idea what your data looks like other than your depiction of it.

    Based on your "gettype()" results, here's one possibility of your variable $a:

      $a = @(
          @(1,2,3),
          @(7,8,9)
      )

      $a.gettype()


      IsPublic IsSerial Name                                     BaseType
      -------- -------- ----                                     --------
      True     True     Object[]                                 System.Array

      $a[0].gettype()


      IsPublic IsSerial Name                                     BaseType
      -------- -------- ----                                     --------
      True     True     Object[]                                 System.Array

      $a[0][0].gettype()

      IsPublic IsSerial Name                                     BaseType
      -------- -------- ----                                     --------
      True     True     Int32                                    System.ValueType


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposé comme réponse Ian Xue vendredi 3 juillet 2020 03:43
    vendredi 19 juin 2020 19:11
  • Hi, if any of the above replies is helpful, please mark it as answer. It would make the reply easier to be found.

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    jeudi 2 juillet 2020 09:09
  • Unfortunately, till the time there is no solution of this problem, please don't close the thread
    mardi 7 juillet 2020 13:54
  • Unfortunately, till the time there is no solution of this problem, please don't close the thread

    Himanshi, please describe your question clearly.

    I cannot get your question, after 2 times reading.

    PSObject[] type mean that you have array of PSObjects (more then 1 elements inside your variable). Rich desribed this for you with code examples. If you cannot get something, let us know what you tried, what you get, and what you want to receive.


    The opinion expressed by me is not an official position of Microsoft

    mardi 7 juillet 2020 14:37
  • Unless you provide answers to the questions asked of you there probably won't be any solution.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    mardi 7 juillet 2020 14:39
  • Unfortunately, till the time there is no solution of this problem, please don't close the thread

    How do you populate the values in $a? That is, show us how you created the $a variable. 

    Using Rich's example, you just have to reference the property names.  Does that work with your data? What does Get-Member see?

    $a = @(
        [PSCustomObject]@{name='Andra';college='JKI';Marks=78.9;Address='RRRR';Date=(Get-Date '5/15/2020 3:10 PM')},
        [PSCustomObject]@{name='Prakash';college='TYU';Marks=67.9;Address='EWSR';Date=(Get-Date '6/15/2019 6:10 PM')},
        [PSCustomObject]@{name='Amit';college='ERT';Marks=88.9;Address='RRFR';Date=(Get-Date '6/15/2020 7:10 AM')},
        [PSCustomObject]@{name='Nobel';college='VFR';Marks=33.9;Address='TTT';Date=(Get-Date '2/15/2020 3:10 PM')}
    )
    $b = $a |  Where-Object name -like "Amit*"
    "{0} attends {1} and scored {2}" -f $b.name, $b.college, $b.Marks
    $b| Get-Member
    $b | Format-Table


    • Modifié MotoX80 mardi 7 juillet 2020 14:50
    mardi 7 juillet 2020 14:48