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?
The Splatting Operator To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol.
& is the call operator which allows you to execute a command, a script, or a function.
In Powershell the question mark is an alias of Where-Object cmdlet: The '?' symbol and Where are both aliases for Where-Object. While $_ is the result/output from the pipeline as stated here. Contains the current object in the pipeline object.
There are no separate math functions in PowerShell. So when you need to access a maths function such as Round or Sqrt, just call for the System. Math. If you have ever wondered about the PowerShell double colon (::), then all is revealed, it means a static method.
%
is an alias for the ForEach-Object
cmdlet.
An alias is just another name by which you can reference a cmdlet or function. For example dir
, ls
, and gci
are all aliases for 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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With