为什么where对象在这种情况下不能工作?
$controlFlowArgs = @{ waitForEnter = $false }
$controlFlowArgs | Format-Table
$controlFlowArgs | Format-List
$result = $controlFlowArgs | Where-Object -FilterScript { $_.Name -eq "waitForEnter" }
$result
输出
Name Value # Format-Table
---- -----
waitForEnter False
Name : waitForEnter # Format-List
Value : False
# Missing result would be here
$controlFlowArgs是一个哈希表。你可能应该换一种想法。
$result = $controlFlowArgs | Where-Object { $_["waitForEnter"] }
将把CCD_ 1存储在CCD_。否则,您可以直接使用哈希表:
if ($controlFlowArgs["waitForEnter"]) {
...
}
对象工作正常的地方。在本例中,仅显示"joe"哈希表。令人困惑的是,格式占据了两行。
@{name='joe';address='here'},@{name='john';address='there'} | ? name -eq joe
Name Value
---- -----
name joe
address here
它仍然被认为是一件事:
@{name='joe';address='here'},@{name='john';address='there'} | ? name -eq joe |
measure-object | % count
1
如果您想要值本身,请使用foreach对象(或选择object-expandproperty(:
@{name='joe';address='here'},@{name='john';address='there'} | ? name -eq joe |
% name
joe
通常powershell与pscustomwobjects:一起工作
[pscustomobject]@{name='joe';address='here'},
[pscustomobject]@{name='john';address='there'} | ? name -eq joe
name address
---- -------
joe here