powershell - Powershell : How to check the good execution of a batch file on a remote computer using invoke-command

标签 powershell error-handling powershell-3.0 powershell-remoting error-code

这是相关的代码:

$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/

相关文章:

.net - Powershell XMLDocument 保存为没有 BOM 的 UTF-8

error-handling - VS 2017 C1083无法打开包含文件:'pcl/io/openni2_grabber.h'

c++ - 单个进程出错后如何正确退出 MPI 应用程序

string - 如何在powershell中检查字符串是否包含数字

powershell - 如何确定是否已在 Powershell 中调用函数

powershell - 清理包含域的巨大文本文件

powershell - Powershell:在输出中组合变量

Azure Function PowerShell 检索和解析 HTTP POST 触发器正文

regex - 为什么这个正则表达式无法匹配第二个 6 位数字?

matlab - 在 MATLAB 中将警告转换为错误