為什么在這種情況下 where-object 不起作用?
$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
uj5u.com熱心網友回復:
$controlFlowArgs 是一個哈希表。你可能應該有不同的想法。
$result = $controlFlowArgs | Where-Object { $_["waitForEnter"] }
將存盤$false在$result. 否則,您可以直接使用 Hashtable:
if ($controlFlowArgs["waitForEnter"]) {
...
}
uj5u.com熱心網友回復:
Where-object 作業正常。在這個例子中,只出現了 '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(或 select-object -expandproperty):
@{name='joe';address='here'},@{name='john';address='there'} | ? name -eq joe |
% name
joe
通常 powershell 與 pscustomobjects 一起使用:
[pscustomobject]@{name='joe';address='here'},
[pscustomobject]@{name='john';address='there'} | ? name -eq joe
name address
---- -------
joe here
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/370391.html
標籤:电源外壳
