gpt4 book ai didi

powershell - 在 Powershell 的 ForEach-Object 循环中获取当前哈希键

转载 作者:行者123 更新时间:2023-12-03 15:10:09 26 4
gpt4 key购买 nike

我有一个哈希表:

$myHash = @{ 
"key1" = @{
"Entry 1" = "one"
"Entry 2" = "two"
}
"key 2" = @{
"Entry 1" = "three"
"Entry 2" = "four"
}
}

我正在循环获取对象:

$myHash.keys | ForEach-Object {
Write-Host $_["Entry 1"]
}

工作正常,但我可以用什么来找出 $myHash 的哪些键算我一个? $_.Name不返回任何东西。我难住了。帮助?

最佳答案

我喜欢用 GetEnumerator()循环哈希表时。它会给你一个属性value带有对象和属性 key用它的 key /名字。尝试:

$myHash.GetEnumerator() | % { 
Write-Host "Current hashtable is: $($_.key)"
Write-Host "Value of Entry 1 is: $($_.value["Entry 1"])"
}

关于powershell - 在 Powershell 的 ForEach-Object 循环中获取当前哈希键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14880725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com