powershell 中的条件执行(&& 和 ||)
已经有一个问题正在解决我的问题(我可以 && 在 Powershell 中工作吗?),但有一点不同。我需要两个命令的输出。看,如果我只是运行:
(command1 -arg1 -arg2) -and (command2 -arg1)
我不会看到任何输出,但会看到 stderr 消息。并且,正如预期的那样,只需输入:
command1 -arg1 -arg2 -and command2 -arg1
就会出现语法错误。
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 messages. And, as expected, just typing:
command1 -arg1 -arg2 -and command2 -arg1
Gives syntax error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
2019:Powershell 团队考虑添加对
&& 的支持;
到 Powershell - 在此 GitHub PR 中权衡尝试一下:
$()
是一个子表达式,允许您在管道中指定多个语句。然后执行命令并通过管道传输到Out-Host
,以便您可以看到它。下一条语句(子表达式的实际输出)应输出$?
即最后一个命令的成功结果。$?
对于本机命令(控制台 exe)工作得很好,但对于 cmdlet 来说,它还有一些不足之处。也就是说,当 cmdlet 遇到终止错误时,$?
似乎仅返回$false
。似乎$?
需要至少三种状态(失败、成功和部分成功)。因此,如果您使用 cmdlet,则效果更好:这种打击仍然存在。也许这样的事情会更好:
2019: the Powershell team are considering adding support for
&&
to Powershell - weigh in at this GitHub PRTry this:
The
$()
is a subexpression allowing you to specify multiple statements within including a pipeline. Then execute the command and pipe toOut-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:This kind of blows still. Perhaps something like this would be better:
Powershell 7 预览版 5 有它们。
我不知道为什么在没有通知或解释的情况下删除了此内容。
https://devblogs.microsoft.com/powershell/powershell-7-preview- 5/ 这将按照问题的要求给出两个命令的输出。
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.
简化多步骤脚本,其中 doThis || exit 1 非常有用,我使用类似的东西:
To simplify multistep scripts where doThis || exit 1 would be really useful, I use something like:
更新:PowerShell [Core] 7.0 引入了
&&
和||
支持 - 请参阅 这个答案。Bash 的 /
cmd
的&&
和||
控制运算符没有 Windows PowerShell 等效项,并且从那时起您无法定义自定义运算符,没有好的解决方法。在此答案中查找背景信息。
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.| 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.
使用
最简单的解决方案是在 cmd shell 中 。当然,您不能在 .ps1 脚本中使用它,因此存在这个限制。
The simplest solution is to use
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
随着 Powershell 7.0 的发布,支持
&&
和||
https://devblogs.microsoft.com/powershell/announcing-powershell-7-0/
With Powershell 7.0 released,
&&
and||
are supportedhttps://devblogs.microsoft.com/powershell/announcing-powershell-7-0/