conditional execution (&& and ||) in powershell
There's already question addressing my issue (Can I get && to work in Powershell?), but with one difference. I need an OUTPUT from both commands. See, if I just run:
(command1 -arg1 -arg2) -and (command2 -arg1)
I won't see any output, but stderr messag开发者_StackOverflow社区es. And, as expected, just typing:
command1 -arg1 -arg2 -and command2 -arg1
Gives syntax error.
2019: the Powershell team are considering adding support for &&
to Powershell - weigh in at this GitHub PR
Try this:
$(command -arg1 -arg2 | Out-Host;$?) -and $(command2 -arg1 | Out-Host;$?)
The $()
is a subexpression allowing you to specify multiple statements within including a pipeline. Then execute the command and pipe to Out-Host
so you can see it. The next statement (the actual output of the subexpression) should output $?
i.e. the last command's success result.
The $?
works fine for native commands (console exe's) but for cmdlets it leaves something to be desired. That is, $?
only seems to return $false
when a cmdlet encounters a terminating error. Seems like $?
needs at least three states (failed, succeeded and partially succeeded). So if you're using cmdlets, this works better:
$(command -arg1 -arg2 -ev err | Out-Host;!$err) -and
$(command -arg1 -ev err | Out-Host;!$err)
This kind of blows still. Perhaps something like this would be better:
function ExecuteUntilError([scriptblock[]]$Scriptblock)
{
foreach ($sb in $scriptblock)
{
$prevErr = $error[0]
. $sb
if ($error[0] -ne $prevErr) { break }
}
}
ExecuteUntilError {command -arg1 -arg2},{command2-arg1}
Powershell 7 preview 5 has them. I don't know why this was deleted with no notification or explanation. https://devblogs.microsoft.com/powershell/powershell-7-preview-5/ This will give the output of both commands, as the question requested.
echo 'hello' && echo 'there'
hello
there
echo 'hello' || echo 'there'
hello
To simplify multistep scripts where doThis || exit 1 would be really useful, I use something like:
function ProceedOrExit {
if ($?) { echo "Proceed.." } else { echo "Script FAILED! Exiting.."; exit 1 }
}
doThis; ProceedOrExit
doNext
# or for long doos
doThis
ProceedOrExit
doNext
Update: PowerShell [Core] 7.0 introduced &&
and ||
support - see this answer.
Bash's / cmd
's &&
and ||
control operators have NO Windows PowerShell equivalents, and since you cannot define custom operators, there are no good workarounds.
- The
| Out-Host
-based workaround in Keith Hill's answer is a severely limited in that it can only send normal command output to the console (terminal), preventing the output from being sent on through the pipeline or being captured in a variable or file.
Find background information in this answer.
The simplest solution is to use
powershell command1 && powershell command2
in a cmd shell. Of course, you can't use this in a .ps1 script, so there's that limitation.
Little longer way is see below
try {
hostname
if ($lastexitcode -eq 0) {
ipconfig /all | findstr /i bios
}
} catch {
echo err
} finally {}
With Powershell 7.0 released, &&
and ||
are supported
https://devblogs.microsoft.com/powershell/announcing-powershell-7-0/
New operators:
Ternary operator: a ? b : c
Pipeline chain operators: || and &&
Null coalescing operators: ?? and ??=
精彩评论