从 PowerShell 中的哈希表获取键值时修改值

Modify value when getting value of key from a hashtable in PowerShell

在 PowerShell 中,我有一个这样创建的结构(HashTable 类型):

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

带大括号的字符串 placeholders/tokens 表示实际值应该是什么,以字符串格式存储为对同一对象中其他值的点表示法的引用。

我写了一个函数来获取那个标记化的值并检索实际值应该是什么,它工作得很好。唯一的问题是每次有标记化值时我都必须执行该函数。无论如何我可以简化这个,特别是而不是 运行:

#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

我是否可以通过拉动括号符号自动完成此操作?

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

可能吗?不可能?

你或许可以这样做:

$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"]