Wait for Output from .exe

Welcome Forums General PowerShell Q&A Wait for Output from .exe

This topic contains 15 replies, has 4 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #164572

    Participant
    Topics: 1
    Replies: 5
    Points: 23
    Rank: Member

    Hello,

    i coded a script, which looks into a folder and checks if there is any .txt file in there.

    If there is a .txt document in the folder, an .exe starts, which imports data from the txt into a database. When the .exe finished, it gives a "Finished" out (for a screenshot, press the link at the end).

    After the data got imported, the .txt should get moved into another folder. The problem is, that right now the file even gets moved, when the .exe runs to an error.

    My Question is: Is it possible to use the Output from the .exe (the "Finished"). And say, that the file only gets moved, when that "Finished" comes?

    How can i code that? I appreciate your help.

    https://www.directupload.net/file/d/5506/87uxocyt_png.htm

     

    Kind Regards,

    Ben

  • #164581

    Participant
    Topics: 3
    Replies: 49
    Points: 118
    Helping Hand
    Rank: Participant

    If you have a rough idea how long it takes to complete the import, you could easily just add a wait function before the file gets moved.

    If you could capture the output as a string, you would have to do some extra coding to parse the string or create a customer PSObject.

    If it were me, I would try the wait function first if it's a urgent fix and then work on parsing the string output from the exe.

  • #164584

    Participant
    Topics: 1
    Replies: 5
    Points: 23
    Rank: Member

    First of all thanks for your quick answer! 🙂

     

    I already tried it with the wait function, but that wouldn't solve the problem, that its get moved, even when the data not get imported correctly.

    Do you have a code example for capturing the output as a string?

     

    I appreciate your help.

    Kind Regards,

    Ben

  • #164590

    Participant
    Topics: 3
    Replies: 49
    Points: 118
    Helping Hand
    Rank: Participant

    I might but it will be a little bit before I can respond. I'll have to check my repos for an example.

    In the meantime, check docs.microsoft.com as there may be some better examples there too. That's what I do a lot now when I'm stuck.

  • #164593

    Participant
    Topics: 1
    Replies: 5
    Points: 23
    Rank: Member

    Okay. Thanks a lot 🙂

  • #164596

    Participant
    Topics: 3
    Replies: 49
    Points: 118
    Helping Hand
    Rank: Participant

    Just had a thought, does this EXE pipe out to STDERR or STDOUT? If so it might be pretty easy but I would need to test my theory.

  • #164599

    Participant
    Topics: 1
    Replies: 5
    Points: 23
    Rank: Member

    The .exe is from a colleague. So i am not 100% sure, but i think it is a STDOUT.

  • #164605

    Participant
    Topics: 3
    Replies: 49
    Points: 118
    Helping Hand
    Rank: Participant

    The .exe is from a colleague. So i am not 100% sure, but i think it is a STDOUT.

    OK great then yeah this should work for you.

    about_Redirection

    Give that a read as it has a lot info I'm not going to re-post.

    What you'll do is use redirection to capture that output from the exe, then store that string as a variable. That should allow you to build some logic around the final step in your script.

    Might look something like this:

    Then you could probably use Select-String or similar technique to parse the output. Give it a try and see what you come up with.

  • #164611

    Participant
    Topics: 1
    Replies: 5
    Points: 23
    Rank: Member

    Thanks for your answer. I tried now a bit with the commands, but it just doesn't work. Tommorow i will take more time for this & will respond, if i found any solution.

    But a quick question: With "$StrOutput = [string] (& myApp.exe 2>&1)" i can save the output. And after i saved the Output, i would need to do a if / else?

    I tried it out and it doesnt work. Would you do that with a if / else? Or how would you do that?

     

    Thanks. Kind Regards,

    Ben

  • #164620

    Participant
    Topics: 3
    Replies: 49
    Points: 118
    Helping Hand
    Rank: Participant

    Without being able to play around with the EXE you are using, I'm just spitballing.

    Not sure if this would work but it's how I would begin to play around with the output:

    
    PS>.\HelloWorldFail.exe Hello World! Test Fail #I made this part, I'll link below.
    PS>$StrOutput = [string] (&.\HelloWorldFail.exe) PS>$StrOutput | Select-String -Pattern 'Pass' -SimpleMatch PS>$StrMatch = $StrOutput | Select-String -Pattern 'Pass' -SimpleMatch
    PS>$StrMatch #this should return null, because the string doesn't contain 'Pass'
    PS>
    PS>$StrMatch -eq $true
    PS>False
    
    

    With that you could build logic based on the True/False value with a if/else statement.

    For reference, here's how you can build an EXE with PowerShell. Be warned, you will have to modify the source PS1 prior to running on a system with PowerShell 5.0 and higher, otherwise it doesn't work.

    https://redmondmag.com/articles/2017/01/27/convert-a-powershell-script-into-an-exe-file.aspx

    [source code]

    https://gallery.technet.microsoft.com/PS2EXE-Convert-PowerShell-9e4e07f1

     

    Good luck!

  • #164623

    Participant
    Topics: 3
    Replies: 49
    Points: 118
    Helping Hand
    Rank: Participant

    Without being able to play around with the EXE you are using, I'm just spitballing.

    Not sure if this would work but it's how I would begin to play around with the output:

    PowerShell
    9 lines

    1
    2
    3
    4
    5
    6
    7
    8
    9
    PS>.\HelloWorldFail.exe Hello World! Test Fail #I made this part, I'll link below.
    PS>$StrOutput = [string] (&.\HelloWorldFail.exe) PS>$StrOutput | Select-String Pattern 'Pass' SimpleMatch PS>$StrMatch = $StrOutput | Select-String Pattern 'Pass' SimpleMatch
    PS>$StrMatch #this should return null, because the string doesn't contain 'Pass'
    PS>
    PS>$StrMatch -eq $true
    PS>False
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    With that you could build logic based on the True/False value with a if/else statement.

    For reference, here's how you can build an EXE with PowerShell. Be warned, you will have to modify the source PS1 prior to running on a system with PowerShell 5.0 and higher, otherwise it doesn't work.

    https://redmondmag.com/articles/2017/01/27/convert-a-powershell-script-into-an-exe-file.aspx

    [source code]

    https://gallery.technet.microsoft.com/PS2EXE-Convert-PowerShell-9e4e07f1

    Good luck!

    forgot to mention that the EXE I created was just a PowerShell script with

    Write-Host

    in it, so I wasn't redirecting anything in an effort to keep it simple.

  • #164626

    Participant
    Topics: 3
    Replies: 49
    Points: 118
    Helping Hand
    Rank: Participant

    Well I'm not getting the results I hoped for. Further testing keeps giving me false output, So I may be completely wrong here. I'm going to keep tinkering.

  • #164629

    Participant
    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    You don't show any code on how you are starting this effort.

    Each of the items you outline is a separate process.

    1 – create a txt file
    2 – start and exe. to read the text file
    3 – call a DB to insert records
    4 – when the insert is complete, move the file.

    So, the questions become,

    1 – How does the DB show it is done?
    2 – How does the exe know that the DB insert is complete?
    3 – How does the DB report insert errors?
    4 – Why would the exe fail for this?
    5 – Is the exe actually doing the insert are calling DB code to do the insert?
    6 – What is the output from the exe, if it is not physical it's just screen buffer stuff and you can't read that.
    7 – Why are you not looking for the exit code? Exe's normally will report err 0 or some other error for success or failure.

    Since you are only doing a read of a text file, no file handle is in place for you to track when it was released. So, you can't used that.  You cannot use the -wait, because all the process actions are not by the exe alone.

    Get Legacy Exit Codes in PowerShell

    PowerShell Is King – Run a EXE inside with style and exit code control

    PowerShell return value, exit code, or ErrorLevel equivalent

    .\YOUREXE.exe
    if($LASTEXITCODE -eq 0)
    {
        Write-Host "The last PS command executed successfully"
    } 
    else 
    {
        Write-Host "The last PS command failed"
    }

     

     

  • #164716

    Participant
    Topics: 1
    Replies: 5
    Points: 23
    Rank: Member

    Thank you both for your answers. It worked with $LASTEXITCODE 🙂

    And Bill i really appreciate ur effort on my question! Have both a nice day.

     

    Kind Regards,

    Ben

  • #165124

    Participant
    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    No worries, glad we got you going.

  • #165367

    Participant
    Topics: 4
    Replies: 10
    Points: 75
    Rank: Member

    When running EXE's I generally use Adam's Invoke-Process function

You must be logged in to reply to this topic.