Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何获取具有特定值的哈希表键?_Powershell_Powershell 2.0 - Fatal编程技术网

Powershell 如何获取具有特定值的哈希表键?

Powershell 如何获取具有特定值的哈希表键?,powershell,powershell-2.0,Powershell,Powershell 2.0,我有一个哈希表,其中根据值使用键 例如 $ComponentTobeBuild=@{"ComponentNameX"="True"; "ComponentNameXyz"="False"; "SomeComponent"="False"} 我希望得到值为True的键。(我将把密钥作为参数传递给其他脚本) 我试着这样做,但我想我遗漏了一些地方,因为它没有列出键 $($Component

我有一个哈希表,其中根据值使用键

例如

    $ComponentTobeBuild=@{"ComponentNameX"="True";
                          "ComponentNameXyz"="False";
                          "SomeComponent"="False"}
我希望得到值为True的键。(我将把密钥作为参数传递给其他脚本)

我试着这样做,但我想我遗漏了一些地方,因为它没有列出键

$($ComponentToBuild.Keys) | Where-Object { $_.Value -eq "True" }

如何获取表示为True的组件名称?我还想知道哈希表是否是这类工作的明智选择。因为我认为哈希表将主要用于处理值

嗨,这应该是你想要的

$ComponentTobeBuild=@{"ComponentNameX"="Test";
                          "ComponentNameXyz"="False";
                          "SomeComponent"="False"}                    

Foreach ($Key in ($ComponentTobeBuild.GetEnumerator() | Where-Object {$_.Value -eq "Test"}))
{$Key.name}

我知道这很古老,但我在搜索另一个问题时看到了这一点,我想知道你为什么要为枚举数而烦恼。只需按照真实语言中的问题构建代码:

给我所有键,其中值为“True”

$ComponentTobeBuild.Keys | ? { $ComponentTobeBuild[$_] -eq 'True'  }
为了一致性,我会将其封装在
@()
中,以获得一个数组,即使只有一个或没有结果


至于询问者的解决方案,问题是
$\uuu
是一个字符串,
.Value
不是键的值。它根本不存在于字符串中。您必须以
$\uucode>作为键获取哈希表的值,以便对其进行比较。

在PowerShell中筛选哈希表的问题是您无法迭代键值对

假设您有一些收藏:

$collection = @{ 
    a = 'A'; 
    b = 'B'; 
    c = 'C'; 
    d = 'D'; 
    e = 'E';
    ...
}
您希望能够使用

$results = $collection | Where-Object { $_.Key -in ('a', 'b', 'd') }
在PowerShell中,但这是不可能的

因此,您必须退回到使用枚举器:

$results = $collection.GetEnumerator() | Where-Object { $_.Key -in ('a', 'b', 'd') }
$results

Name                           Value
----                           -----
d                              D
b                              B
a                              A

您还可以使用
Dictionary.Keys
Where
方法,该方法返回条件为真的所有元素。这种情况可能是lambda断言被测试的键
$\ucode>与字典中的
“True”
值相关

$ComponentTobeBuild=@{"ComponentNameX"="True";
                      "ComponentNameXyz"="False";
                      "SomeComponent"="False"}
$ComponentTobeBuild.Keys.Where({$ComponentTobeBuild[$_] -eq "True"})
# -> @("ComponentNameX")
我发现最简单(也是最整洁)的方法如下:

$ComponentToBuild.where{$_.Keys -match 'True'}
那就去拿你需要的东西

$ComponentToBuild.where{$_.Keys -match 'True'} | select Field1,Field2,Field99

etc

此脚本正在尝试获取值,实际上我正在尝试获取键。但这样做也不管用$ComponentToBuild.Keys | Where Object{$eq“True”}好的,很抱歉,我更改了代码检查,如果它现在可以工作。ps复制完整的代码并尝试一下,您可以将代码放入“{$Key.name}”部分,将所有具有test值的键放入另一个哈希表中,如果您喜欢这个答案,因为它使用GetEnumerator()的name属性,但我不喜欢foreach()和管道的混合。将Where对象放在foreach语句中,就像()一样,或者只使用管道:
$componentToBuild.GetEnumerator()| Where对象{$\u0.Value-eq“Test”}foreach对象{$\u0.Name}
您也可以使用
$Key。Key
因为
Name
是一个别名属性。当集合中有多个具有相同密钥的条目时,这可能会导致意外行为…@oɯǝɹ在Powershell中,您无法使用非唯一属性创建hastablekeys@maozim我认为我们使用hashtable这个术语是错误的,因为我们在问题中使用了字典/地图数据结构。net中的哈希表可能会发生冲突,并将使用链表在同一哈希代码下存储多个项。哈希表是关于对象检索的。字典使用唯一键(@{}).GetType().Fullname是System.Collections.HashTable。有人能提供一个基本原理吗?键不能像其他集合一样迭代或索引?它的类型是Hashtable+keyCollection,这至少意味着它是一种集合。
$ComponentToBuild.where{$_.Keys -match 'True'} | select Field1,Field2,Field99