首页 > 解决方案 > PowerShell 中的调用表达式无法识别运算符

问题描述

我正在尝试在 PowerShell 脚本中运行docker命令并测试该命令是否不成功,但我没有这样做。我在用:

if (!Invoke-Expression -Command docker ps -q -f name=php72) {
    #some other code here
}

运行后我得到的错误是:

Line |
  31 |  if (!Invoke-Expression -Command docker ps -q -f name=php72) {
     |      ~~~~~~~~~~~~~~~~~~
     | The term '!Invoke-Expression' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a
     | path was included, verify that the path is correct and try again.

我也尝试过-not运营商无济于事。

标签: powershell

解决方案


添加另一个括号:

if (!(Invoke-Expression -Command docker ps -q -f name=php72)){
    #some other code here
}

除非这里!Invoke-Expression被评估,这是无效的。

最好的方法是这样使用:

docker ps -q -f name=php72 >$null 2>&1
if($lastExitCode -eq 1){"FAILED!"}

推荐阅读