0

In PowerShell, I have a structure (HashTable type) created like so:

$Structure = @{
    "KeyOne" = "Value"
    "KeyTwo" = "{Change.This.That}"
    "KeyThree" = "{Change.This.Thing} With Stuff"
    "Change" = @{
        "This" = @{
            "That" = "Another Value"
            "Thing" = "Yogurt"
        }
    }
}

The string with the curly brackets are placeholders/tokens for what the actual value should really be, stored in a string format as a reference to the dot notation of some other value within the same object.

I wrote a function to take that tokenized value and retrieve what the actual value is supposed to be that works perfectly fine. Only problem is I have to execute the function every time there is a tokenized value. Is there anyway I can simplify this, specifically instead of running:

#I pass structure because I'd rather avoid assuming 
#the hashtable will always be called $Structure
Convert-Token -String $Structure.KeyTwo -Obj $Structure #Another Value

is there anyway I can automatically do this by just pulling the bracket notation?

#Like this?
$Value = $Structure["KeyThree"]
$Value #Yogurt With Stuff

Possible? Not possible?

1 Answer 1

1

You can probably do something like this:

$Selector = $Structure

$Structure = @{
    "KeyOne" = "Value"
    "KeyTwo" = "$($Selector.Change.This.That)"
    "KeyThree" = "$($Selector.Change.This.Thing) with Stuff"
    "Change" = @{
        "This" = @{
            "That" = "Another Value"
            "Thing" = "Yogurt"
        }
    }
}


$Structure["KeyThree"]
Sign up to request clarification or add additional context in comments.

1 Comment

Well...that was just too easy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.