0

我正在尝试执行一个 sript 来运行 exe 文件,获取输出,搜索输出,然后如果它是真的做一些事情:

$output = cmd /c file.exe additionalvariables --batch | 
    write-host | 
    where-object($_ -eq "Finished  with Success") #finished with success does not work

if ( -eq "Finished  with Success") # I need to perform a check
{
    "Command executed"
    $tcp.Dispose()
    Exit 0
}
else
{
    "There is an issue with file.exe additionalvariables command"
    EXIT 1
    $tcp.Dispose()
}

所以finished with success在第1行不起作用,你知道如何检查if语句吗?if ( -eq "Finished with Success").

4

1 回答 1

0

通常,您希望 .exe 返回零 (0) 表示成功,返回零 (0) 以外的任何值表示失败。通常不需要解析文本输出。下面是一些可能让你开始做某事的代码。

$output = cmd /c returnit.bat 0
$LASTEXITCODE
Write-Verbose "===$output==="

$output.gettype()

if ($output -match '.*Finished with Success.*') {
    "Command executed"
    $tcp.Dispose()
    Exit 0
}
else
{
    "There is an issue with file.exe additionalvariables command"
    EXIT 1
    $tcp.Dispose()
}

=== returnit.bat

@ECHO OFF
SET /A "IT=0"
IF "%1" NEQ "" (SET /A "IT=%1")
IF %IT% EQU 0 (
    ECHO a line
    ECHO Finished with Success
    ECHO another line
)
EXIT /B %IT%
于 2016-11-09T20:30:46.537 回答