Invoke-Expression
将返回被调用的命令的所有文本。
但是,如何获得系统返回值是否已成功执行此命令或失败?在CMD中,我可以使用%errorlevel%
来获取外部命令执行状态。 PowerShell怎么样?
通常你会使用$?
检查最后执行的语句的状态:
PS C:\> Write-Output 123 | Out-Null; $?
True
PS C:\> Non-ExistingCmdlet 123 | Out-Null; $?
False
但是,这不适用于Invoke-Expression
,因为即使传递给Invoke-Expression
的表达式中的语句可能会失败,Invoke-Expression
调用它自己也会成功(即表达式,尽管无效/非功能被调用)
使用Invoke-Expression
你将不得不使用try:
try {
Invoke-Expression "Do-ErrorProneAction -Parameter $argument"
} catch {
# error handling go here, $_ contains the error record
}
或陷阱:
trap {
# error handling goes here, $_ contains the error record
}
Invoke-Expression "More-ErrorProneActions"
另一种方法是将";$?"
附加到要调用的表达式:
$Expr = "Write-Host $SomeValue"
$Expr += ';$?'
$Success = Invoke-Expression $Expr
if(-not $Success){
# seems to have failed
}
但依赖于没有任何管道输出
在PowerShell中,您可以通过检查automatic variables来评估执行状态
$?
Contains True if last operation succeeded and False otherwise.
和/或
$LASTEXITCODE
Contains the exit code of the last Win32 executable execution.
前者用于PowerShell cmdlet,后者用于外部命令(如批处理脚本中的%errorlevel%
)。
这对你有帮助吗?
$ LASTEXITCODE不能与Invoke-Expression一起使用,因为无论调用的表达式是成功还是失败,它都将为零:
PS C:\Users\myUserAccount> touch temp.txt
PS C:\Users\myUserAccount> Invoke-Expression "Remove-Item .\temp.txt"
PS C:\Users\myUserAccount> echo $LASTEXITCODE
0
PS C:\Users\myUserAccount> Invoke-Expression "Remove-Item .\temp.txt"
Remove-Item : Cannot find path 'C:\Users\myUserAccount\temp.txt' because it does not
exist.
At line:1 char:1
+ Remove-Item .\temp.txt
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\myUserAccount\temp.txt:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
PS C:\Users\myUserAccount> echo $LASTEXITCODE
0
如果Invoke-Expression
调用的可执行文件支持它,则可以使用$LASTEXITCODE
。但是,您必须小心变量范围。
function foo
{
$global:LASTEXITCODE = 0 # Note the global prefix.
Invoke-Expression "dotnet build xyz" # xyz is meaningless to force nonzero exit code.
Write-Host $LASTEXITCODE
}
foo
如果你运行它,输出将是:
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
MSBUILD : error MSB1009: Project file does not exist.
Switch: xyz
1
观察末尾的1表示非零退出代码。
如果你忘记了global:
前缀,那么输出就会有0.我相信这是因为LASTEXITCODE
的函数范围定义会隐藏全局设置的。