2016-11-09 54 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%