2016-11-09 100 views
0

我试图执行一个sript运行exe文件,获取输出,通过输出搜索,然后如果真做soemthing:POWERSHELL:管道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() 
} 

所以成品成功在第1行不起作用,你知道如何检查陈述吗? if (-eq "Finished with Success")

+1

什么是$ LASTEXITCODE的.exe文件完成后的价值? – lit

+0

输出结果如何?举个例子 –

+0

'where-object($ _ -eq“Finished with Success”)'应该是'where-object {$ _ -eq“Finished with Success”}'''write-host' should not be used as you正在将数据发送到另一个流。只要删除管道的那一部分,然后再试一次。 – Matt

回答

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%