我正在尝试在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有2个parsing modes:表达式和参数。后者是您使用命令的地方。此处,命令名称(例如Invoke-Expression
)必须始终排在最前面。
简而言之:您必须使用grouping opterator ()
或subexpression operator $()
包装命令,以在表达式中使用它:
-not (Invoke-Expression ...)
# or
-not $(Invoke-Expression ...)
(-not
和!
与same完全一样)但是正如@WasifHasan指出的那样,docker是一个外部程序,所以检查是否成功的最佳方法是使用
$?
automatic variable,如果$false
是$LASTEXITCODE
以外的任何其他值,则它将是0
:docker ps -q -f name=php72 2>&1>$null
if ($?} {
# your other code here
}
关于powershell - 无法在PowerShell中使用Invoke-Expression识别运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64409103/