【问题标题】:Modify value when getting value of key from a hashtable in PowerShell从PowerShell中的哈希表获取键值时修改值
【发布时间】:2015-07-05 09:56:59
【问题描述】:

在 PowerShell 中,我创建了一个结构(HashTable 类型),如下所示:

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

带有大括号的字符串是实际值的占位符/标记,以字符串格式存储作为对同一对象中其他值的点表示法的引用。

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

#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

可能吗?不可能?

【问题讨论】:

    标签: powershell hashtable


    【解决方案1】:

    您可能可以这样做:

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

    【讨论】:

    • 嗯...这太容易了
    猜你喜欢
    • 1970-01-01
    • 2015-05-26
    • 2017-02-06
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多