According to documentation, PS 7 has introduced pipeline chaining operators such as ||
and &&
.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipeline_chain_operators?view=powershell-7
And you should be able to do C#-style short circuiting operations like:
Write-Error 'Bad' && Write-Output 'Second'
The above examples work. And the documentation says that the pipeline chaining operators use two fields (unsure how that works precisely): $?
and $LASTEXITCODE
How do I apply these to my own functions?
For example:
function yes() {
echo 'called yes'
return $True
}
function no() {
echo 'called no'
return $False
}
I feel like I should be able to run the following no && yes
and see the following output
called no
False
but instead I see
called no
False
called yes
True
So how do I develop functions in such a way that I can use pipeline chaining and short circuiting?
edit: the only way I can figure out right now to construct a custom function that will short circuit an &&
is to make it throw
, but that doesn't seem too useful in the general case.