Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Set pscustomobject to return only a specific property

$
0
0

Hi

I don't know if it possible or make sense but is it possible to make an object return only one of its properties when called?

What I mean is, I want to create a "tree" of objects (for example a tree of "DateTime" objects)

I can do it like this:

$Level3 = [pscustomobject]@{
  'Value' = Get-Date
}

$Level2v1 = [pscustomobject]@{
  'Value' = Get-Date
}

$Level2v2 = [pscustomobject]@{
  'Value' = Get-Date
  'Level3' = $Level3
}

$Level1 = [pscustomobject]@{
  'Value' = Get-Date
  'Level2v1' = $Level2v1
  'Level2v2' = $Level2v2
}

Then later I can get the specific data (the DateTime objects) I need like this:

$Level1.Value
$Level1.Level2v2.Level3.Value
$Level1.Level2v2.Level3.Value.GetType().Name

Output:

Tuesday, June 7, 2016 11:03:11 PM
Tuesday, June 7, 2016 11:03:12 PM
DateTime

Can I get the same results without writing "Value" at the end of the lines when I retrieve the data like this:

$Level1.Level2v2.Level3

Maybe by adding a Method or a script or something to my customobject?

Thank you



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>