Vasanth
Vasanth

Reputation: 25

Select and Iterate over the Properties

I'm trying to read a content from the list of properties with one of the property being an object. What do I need to use to read a string and object properties?

Here is the output of Get-Member of the list

PS> $r | Get-Member

Name              MemberType Definition
----              ---------- ----------
Equals            Method     bool Equals(System.Object obj)
GetHashCode       Method     int GetHashCode()
GetType           Method     type GetType()
ToString          Method     string ToString()
ActivityName      Property   string ActivityName {get;}
DurationInMs      Property   System.Nullable[int] DurationInMs {get;}
Error             Property   System.Object Error {get;}
Input             Property   System.Object Input {get;}
Output            Property   System.Object Output {get;}
Status            Property   string Status {get;}


PS> $r.Output | Get-Member

...
Name            Property       string Name {get;}
Next            Property       Newtonsoft.Json.Linq.JToken Next {get;}
Path            Property       string Path {get;}
Previous        Property       Newtonsoft.Json.Linq.JToken Previous {get;}
Root            Property       Newtonsoft.Json.Linq.JToken Root {get;}
...

(It's a long list, hiding unnecessary details)

I am trying to select ActivityName, DurationInMs and Values of Output in Table format. The Root Property in Output object has values for dataRead, dataWritten properties.

This is what I tried:

$r | Select ActivityName,DurationInMs

Output:

ActivityName     DurationInMs
------------     ------------
SetDay                     78
Set TS                     15
LKUP_FileControl        12206
CopyPaths               38585

I also tried this:

$r.Output | select @{name="dataRead"; expression={$_.Root.dataRead.Value}},@{name="dataWritten"; expression={$_.Root.dataWritten.Value}}

Output:

dataRead     dataWritten
--------     -----------


520114512    520114512
520114512    520114512
520114512    520114512
0            0
0            0
0            0
3199091788   3199091788
3199091788   3199091788
3199091788   3199091788

(Note: This is resulting in duplicates as the Root property has other nested properties inside)

I expect the output to be in table format, like:

ActivityName     DurationInMs    dataRead      dataWritten
------------     ------------    ---------    ------------
SetDay                     78
Set TS                     15    520114512       520114512
LKUP_FileControl        12206            0               0
CopyPaths               38585   3199091788      3199091788

How can I do this in PowerShell?

Upvotes: 2

Views: 265

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200283

Combine your two code snippets to get the desired result:

$r | Select-Object ActivityName, DurationInMs,
    @{n='dataRead';e={$_.Output.Root.dataRead.Value}},
    @{n='dataWritten';e={$_.Output.Root.dataWritten.Value}}

Upvotes: 0

Related Questions