【问题标题】:What does `?{}` Represent in PowerShell`?{}` 在 PowerShell 中表示什么
【发布时间】:2013-09-04 19:20:05
【问题描述】:
我在PowerShell 中有以下表达式:
$oneTypes = Get-ChildItem -Path $Location -Directory
$onlyCompile = @( $BASE_A, $BASE_B, $BASE_C )
#trying figure this line out
$oneTypes = ($oneTypes | ?{$onlyCompile -contains $_})
我不确定? 和{...} 在这里做什么?看起来它可能是一个脚本块,但我不确定。我也想知道管道是如何在这方面发挥作用的。
【问题讨论】:
标签:
.net
windows
powershell
【解决方案1】:
? 用作Where-Object 命令的简写形式,这里表示过滤提供的数组$onlycompile 中的所有对象
【解决方案2】:
您问题另一部分的答案是{...} 在? 或Where 之后确实代表一个脚本块。如果您查看有关 Where-Object 的帮助,您会看到:
-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
FilterScript 参数需要一个脚本块。它是定位的,因此您不必指定Where -FilterScript { ... }。它不是管道绑定的,但InputObject 参数是管道绑定的。该对象被注入到您提供为$_ 的脚本块中。