0

我有以下表达式PowerShell

$oneTypes = Get-ChildItem -Path $Location -Directory


$onlyCompile = @( $BASE_A, $BASE_B, $BASE_C )
#trying figure this line  out
$oneTypes = ($oneTypes | ?{$onlyCompile -contains $_})

我不确定?{...}在这里做什么?看起来它可能是一个脚本块,但我不确定。我也想知道管道是如何在这方面发挥作用的。

4

2 回答 2

5

?用作Where-Object命令的缩写形式,在这里它意味着过滤提供的数组中的所有对象$onlycompile

于 2013-09-04T19:29:33.387 回答
2

The answer to the other part of your question is that {...} after the ? or Where does indeed represent a scriptblock. If you look at the help on Where-Object you see this:

-FilterScript <ScriptBlock>
    Specifies the script block that is used to filter the objects. Enclose the script block in braces ( {} ).

    The parameter name (-FilterScript) is optional.

    Required?                    true
    Position?                    1
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false

The FilterScript parameter expects a scriptblock. It is positional so you don't have to specify Where -FilterScript { ... }. It is not pipeline bound but the InputObject parameter is pipeline bound. That object gets injected into the scriptblock you provide as $_.

于 2013-09-04T21:17:24.133 回答