powershell 中的条件执行(&& 和 ||)

发布于 2024-08-21 11:21:09 字数 399 浏览 3 评论 0原文

已经有一个问题正在解决我的问题(我可以 && 在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

翻身的咸鱼 2024-08-28 11:21:09

2019:Powershell 团队考虑添加对 && 的支持; 到 Powershell - 在此 GitHub PR 中权衡

尝试一下:

$(command -arg1 -arg2 | Out-Host;$?) -and $(command2 -arg1 | Out-Host;$?)

$() 是一个子表达式,允许您在管道中指定多个语句。然后执行命令并通过管道传输到 Out-Host,以便您可以看到它。下一条语句(子表达式的实际输出)应输出 $? 即最后一个命令的成功结果。


$? 对于本机命令(控制台 exe)工作得很好,但对于 cmdlet 来说,它还有一些不足之处。也就是说,当 cmdlet 遇到终止错误时,$? 似乎仅返回 $false。似乎 $? 需要至少三种状态(失败、成功和部分成功)。因此,如果您使用 cmdlet,则效果更好:

$(command -arg1 -arg2 -ev err | Out-Host;!$err) -and 
$(command -arg1 -ev err | Out-Host;!$err)

这种打击仍然存在。也许这样的事情会更好:

function ExecuteUntilError([scriptblock[]]$Scriptblock)
{
    foreach ($sb in $scriptblock)
    {
        $prevErr = $error[0]
        . $sb
        if ($error[0] -ne $prevErr) { break }
    }
}

ExecuteUntilError {command -arg1 -arg2},{command2-arg1}

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}
一身骄傲 2024-08-28 11:21:09

Powershell 7 预览版 5 有它们。
我不知道为什么在没有通知或解释的情况下删除了此内容。
https://devblogs.microsoft.com/powershell/powershell-7-preview- 5/ 这将按照问题的要求给出两个命令的输出。

echo 'hello' && echo 'there'
hello
there

echo 'hello' || echo 'there'
hello

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
醉城メ夜风 2024-08-28 11:21:09

简化多步骤脚本,其中 doThis || exit 1 非常有用,我使用类似的东西:

function ProceedOrExit { 
    if ($?) { echo "Proceed.." } else { echo "Script FAILED! Exiting.."; exit 1 } 
}

doThis; ProceedOrExit
doNext

# or for long doos
doThis
ProceedOrExit
doNext

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
旧竹 2024-08-28 11:21:09

更新PowerShell [Core] 7.0 引入了 &&|| 支持 - 请参阅 这个答案


Bash 的 / cmd&&|| 控制运算符没有 Windows PowerShell 等效项,并且从那时起您无法定义自定义运算符,没有好的解决方法

  • <代码>| Keith Hill 的答案中基于 Out-Host 的解决方法受到严格限制,因为它只能发送正常命令输出到控制台(终端),防止输出通过管道发送或被捕获到变量或文件中。

此答案中查找背景信息。

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.

青朷 2024-08-28 11:21:09

使用

powershell command1 && powershell command2

最简单的解决方案是在 cmd shell 中 。当然,您不能在 .ps1 脚本中使用它,因此存在这个限制。

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.

执妄 2024-08-28 11:21:09

更长一点的路见下文

try {
  hostname
  if ($lastexitcode -eq 0) {
    ipconfig /all | findstr /i bios
  }
} catch {
  echo err
} finally {}

Little longer way is see below

try {
  hostname
  if ($lastexitcode -eq 0) {
    ipconfig /all | findstr /i bios
  }
} catch {
  echo err
} finally {}
猫弦 2024-08-28 11:21:09

随着 Powershell 7.0 的发布,支持 &&||

https://devblogs.microsoft.com/powershell/announcing-powershell-7-0/

New operators:
  Ternary operator: a ? b : c
  Pipeline chain operators: || and &&
  Null coalescing operators: ?? and ??=

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 ??=
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文