There is a hashtable:
$Main = @{
One = 'One Value'
Two = @{
Two = 'Two Value'
Tree = @{
Tree = 'Tree Value'
}
}
}
Why can't I get the values this way?
# It works
$Want = 'One'
$Main.$Want
# This does not work. I get a void
$Want = 'Two.Two'
$Main.$Want
# And that of course doesn't work either
$Want = 'Two.Tree.Tree'
$Main.$Want
How to modify $Want
string so that it turns out to be possible (for example, [Something]$Want = 'Two.Tree.Tree'
and I get the value $Main.$Want
of Tree Value
).
At the moment, I have made such a decision. But for sure there are shorter and faster solutions provided.
$Main = @{
One = 'One Value'
Two = @{
Two = 'Two Value'
Tree = @{
Tree = 'Tree Value'
}
}
}
# $Want = 'One'
# $Want = 'Two.Two'
$Want = 'Two.Tree.Tree'
$Want = $Want -Split '\.'
$Want = Switch ( $Want.Length ) {
1 { $Main.($Want[0]) }
2 { $Main.($Want[0]).($Want[1]) }
3 { $Main.($Want[0]).($Want[1]).($Want[2]) }
}
$Want
# Result 'Tree Value'
Use Invoke-Expression:
$Want = 'Two.Two'
Invoke-Expression ('$Main.' + $Want)