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

时间:2016-11-09 15:15:17

标签: powershell write-host

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

$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()
}

所以成功完成在第1行不起作用,你知道怎么检查if语句吗? if ( -eq "Finished with Success")

1 个答案:

答案 0 :(得分: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%