0

I am trying to execute a sript to run exe file, get the output, search through the output and then if its true do 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()
}

So the finished with success does not work in line 1, do you know how to check if statement? if ( -eq "Finished with Success").

Matt
  • 45,022
  • 8
  • 78
  • 119
user5711825
  • 85
  • 1
  • 1
  • 11
  • 1
    What is the value of $LASTEXITCODE after the .exe completes? – lit Nov 09 '16 at 15:27
  • Whats the output look like? Give an example – Kirill Pashkov Nov 09 '16 at 15:32
  • `where-object($_ -eq "Finished with Success")` should be `where-object{$_ -eq "Finished with Success"}` and `write-host` should not be used as you are sending data to another stream. Just remove that part of the pipe and try again. – Matt Nov 09 '16 at 15:33
  • This the output: `Batch interface for EXE component v.4.01.47 Copyright 2015 EXE Execution = 1 Mode = parameter Finished with Success Executing StoredProcedure SP 1 Executing StoredProcedure SP 2` – user5711825 Nov 09 '16 at 15:48
  • why won't you do something like: start-process cmd -ArgumentList '/c file.exe additionalvariables --batch' -RedirectStandardOutput c:\1.txt – 4c74356b41 Nov 09 '16 at 15:51
  • In general, the .exe should return zero (0) if it is successful and something other than zero (0) if there is an error. String matching should not be required. – lit Nov 09 '16 at 15:52
  • yeah, I didnt write this exe, I am just trying to pipe the output then check if it was successful or not. I have also tried without 'write-host |' it seems it is working but now I cannot see any output from the file. It would be very useful to see it as it does operation on the database. – user5711825 Nov 09 '16 at 15:55
  • did you try to redirectoutput like i proposed? – 4c74356b41 Nov 09 '16 at 16:11
  • thanks, I was able to solve my problem by piping it out to file then parsing the file. Its not ideal scenario as I wanted to avoid creating new files but its the simplest. thank you! :) – user5711825 Nov 09 '16 at 16:40

1 Answers1

0

In general, you would expect the .exe to return zero (0) for success and anything other than zero (0) for failure. Parsing the text output is not generally required. Below is some code that might get you started on something.

$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%
lit
  • 14,456
  • 10
  • 65
  • 119