[英]What does |% mean in PowerShell
I've come across this today: 我今天遇到过这个:
$tests = (Get-ChildItem . -Recurse -Name bin |% { Get-ChildItem $_ -Recurse -Filter *.Unit.Tests.dll } |% { $($_.FullName) })
Write-Host ------------- TESTS -----------
Write-Host $tests
Write-Host -------------------------------
.\tools\xunit\xunit.console.exe $tests -xml test-report.unit.xml
I can't get my head around what '|%' is doing there. 我无法理解'|%'在那里做什么。 Can anyone explain what it used for please?
有谁可以解释它用于什么?
%
is an alias for the ForEach-Object
cmdlet . %
是ForEach-Object
cmdlet的别名 。
An alias is just another name by which you can reference a cmdlet or function. 别名只是您可以引用cmdlet或函数的另一个名称。 For example
dir
, ls
, and gci
are all aliases for Get-ChildItem
. 例如,
dir
, ls
和gci
都是Get-ChildItem
别名。
ForEach-Object
executes a [ScriptBlock]
for each item passed to it through the pipeline, so piping the results of one cmdlet into ForEach-Object
lets you run some code against each individual item. ForEach-Object
为通过管道传递给它的每个项执行[ScriptBlock]
,因此将一个cmdlet的结果传递给ForEach-Object
可以让您针对每个单独的项运行一些代码。
In your example, Get-ChildItem
is finding all of the bin
s in a certain directory tree, and then for each one that is found , Get-ChildItem
is being used to find all of the files underneath that match *.Unit.Tests.dll
, and then for each one of those , the full name is returned to the pipeline (which is getting assigned to the $tests
variable). 在您的示例中,
Get-ChildItem
正在查找某个目录树中的所有bin
,然后对于找到的每个 bin
, Get-ChildItem
用于查找匹配*.Unit.Tests.dll
下面的所有文件*.Unit.Tests.dll
,然后对于每一个 ,全名返回到管道(它被分配给$tests
变量)。
Adding to @briantist's answer: 添加到@briantist的答案:
The |
|
is the pipeline operator. 是管道运营商。 From the docs :
来自文档 :
Each pipeline operator sends the results of the preceding command to the next command.
每个管道运算符将前一个命令的结果发送到下一个命令。
To demonstrate: 展示:
Get-ChildItem bin | % { Get-ChildItem $_ }
could be rewritten as: 可以改写为:
Get-ChildItem bin | Foreach-Object { Get-ChildItem $_ }
or without using the pipeline but a foreach loop instead (which is different from the foreach command !): 或者不使用管道而是使用foreach循环 (这与foreach命令 不同 !):
foreach ($item in (Get-ChildItem bin)) { Get-ChildItem $item }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.