POWERSHELL:管道 exe 文件输出到变量

POWERSHELL: piping exe file output in to variable

我正在尝试对 运行 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")

一般来说,您希望 .exe return 零 (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%