How do I list all object property values?

This topic contains 5 replies, has 3 voices, and was last updated by  js 1 week, 2 days ago.

  • Author
    Posts
  • #104092

    Craig Davis
    Participant

    Using the $ERROR object as an example, how would I iterate through each property, expand collections of properties, and get all values? I'd like to have the full output of an error and write it to a log file.
    $ERROR|get-member shows 8 properties
    $ERROR[0].CategoryInfo |Get-Member shows 5 properties
    I'm assuming a recursive loop but have not been able to get it to work.

  • #104096

    js
    Participant

    I don't like how this is setup. You can't tab complete to explore it. You can do:

    $error | fl -force
    
    $error | select *

    This is the only time I've ever used fl -force.

  • #104098

    Don Jones
    Keymaster

    Also try Format-Custom and experiment with the depth parameter.

  • #104101

    Craig Davis
    Participant

    Thanks for the replies. I'm looking to go further, expand properties and get more details.

    For exmaple – If I look at the most recent $Error, $Error[0], I see 9 properties.

      PS > $Error[0].psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----                                                                                                                                                                                                 
      PSMessageDetails                                                                                                                                                                                     
      Exception                                                                                                                                                                                            
      TargetObject                                                                                                                                                                                         
      CategoryInfo                                                                                                                                                                                         
      FullyQualifiedErrorId                                                                                                                                                                                
      ErrorDetails                                                                                                                                                                                         
      InvocationInfo                                                                                                                                                                                       
      ScriptStackTrace                                                                                                                                                                                     
      PipelineIterationInfo 
    

    One of the properties listed is "Exception".
    If I look at properties of "Exception" I see 10 more properties.

      PS > $Error[0].Exception.psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----  
      ErrorRecord
      StackTrace 
      WasThrownFromThrowStatement
      Message
      Data
      InnerException
      TargetSite
      HelpLink
      Source
      HResult
    

    I am looking to loop through each property, and expand properties where there are more properties. Like "select-object -expand" but in a recursive loop so I can explore all properties of an object.

  • #104102

    Craig Davis
    Participant

    Thanks for the replies. I'm looking to go further, expand properties and get more details.

    For example – If I look at the most recent $Error, $Error[0], I see 9 properties.

      PS >; $Error[0].psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----                                                                                                                                                                                                 
      PSMessageDetails                                                                                                                                                                                     
      Exception                                                                                                                                                                                            
      TargetObject                                                                                                                                                                                         
      CategoryInfo                                                                                                                                                                                         
      FullyQualifiedErrorId                                                                                                                                                                                
      ErrorDetails                                                                                                                                                                                         
      InvocationInfo                                                                                                                                                                                       
      ScriptStackTrace                                                                                                                                                                                     
      PipelineIterationInfo 
    

    One of the properties listed is "Exception".
    If I look at properties of "Exception" I see 10 more properties.

      PS > $Error[0].Exception.psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----  
      ErrorRecord
      StackTrace 
      WasThrownFromThrowStatement
      Message
      Data
      InnerException
      TargetSite
      HelpLink
      Source
      HResult
    

    I am looking to loop through each property, and expand properties where there are more properties. Like "select-object -expand" but in a recursive loop so I can explore all properties of an object.

  • #104138

    js
    Participant

    Hmm, weird things happen with get-content. Bruce Payette mentioned in the 2 hr ps arch video that get-content returned more than just text:

    get-content wmi.txt | fl * -force
    
    
    PSPath       : C:\Users\admin\wmi.txt
    PSParentPath : C:\Users\admin
    PSChildName  : wmi.txt
    PSDrive      : C
    PSProvider   : Microsoft.PowerShell.Core\FileSystem
    ReadCount    : 1
    Length       : 0
    
    

You must be logged in to reply to this topic.