0

I can't find the answer to the following question.

Code:

function Get-Sub([string]$select)
{
    $var_string = '{"roles": {"categories": {"test": "111"}}}'
    $psobject = $var_string | ConvertFrom-Json
    $psobject.roles.categories | out-gridview -Title "Working"
    $psobject.$select | out-gridview -Title "Not Working"
}

Get-Sub -select 'roles.categories'

How can i get the parameter selection to work?

Paulus
  • 31
  • 2
  • You need to resolve each sub-property [one at a time](https://stackoverflow.com/a/69368844/712649) – Mathias R. Jessen Feb 14 '23 at 16:44
  • GPTChat just gave me the answer: function Get-Sub([string]$select) { $var_string = '{"roles": {"categories": {"test": "111"}}}' $psobject = $var_string | ConvertFrom-Json $psobject.roles.categories | Out-GridView -Title "Working" $result = Invoke-Expression ('$psobject.' + $select) if ($result -ne $null) { $result | Out-GridView -Title "Now Working" } else { Write-Error "Property '$select' not found" } } Get-Sub -select 'roles.categories' – Paulus Feb 14 '23 at 17:30

0 Answers0