这是相关的代码:
$op=invoke-command -computername test -scriptblock{
& "cmd.exe" /c c:\temp\psbatch.bat 2>&1
$LASTEXITCODE
}
if ($op - eq 0) {echo "success"}
else {echo "failure"}
问题是,如果我的批处理回显某些内容,则输出将在$ op中捕获,因此它将不等于0。
如何处理?
最佳答案
您可以尝试如下操作:
$op=invoke-command -computername test -scriptblock{
& "cmd.exe" /c c:\temp\psbatch.bat 2>&1
$LASTEXITCODE
}
if (@($op)[-1] -eq 0) {echo "success"}
else {echo "failure"}
将结果强制为数组,然后仅测试最后一行,这将是最后一个退出代码。在此之前回声将被忽略。
关于powershell - Powershell : How to check the good execution of a batch file on a remote computer using invoke-command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506749/