I'm writing a Powershell cmdlet to list changesets from TFS. I successfully query TFS and get a collection of changesets but want to return simplified objects that contain only a few properties. I can do that using Select-Object
like this...
$changesets | Select-Object ChangeSetId, Owner, Comment
The last property I would like to add is the Changes
property which is an array of changes. I would like to simplify those objects as well. I'm trying this but it doesn't return what I want...
$changesets | Select-Object `
ChangeSetId,
Owner,
Comment,
@{Name="Changes"; Expression={ $_.Changes | Select-Object ChangeType, ServerItem }}
Is there a way to handle nested collections with Select-Object
?
How do I find the value of a nested object? If you need to search for a nested object, you can use Lodash's . find() function.It takes three arguments: collection : which can be either an array or object.
The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.
Your can use -ExpandProperty
parameter of Select-Object
cmdlet. It will expand collection and add selected properties from parent object to child objects:
$changesets | Select-Object ChangeSetId, Owner, Comment,
@{Name="Changes"; Expression={ $_.Changes | Select-Object ChangeType, ServerItem }} |
Select-Object -Property * -ExcludeProperty Changes -ExpandProperty Changes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With