Wait for Output from .exe

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.

 

Kind Regards,

Ben

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.

First of all thanks for your quick answer! :slight_smile:

 

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

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.

Okay. Thanks a lot :slight_smile:

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.

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

[quote quote=164599]The .exe is from a colleague. So i am not 100% sure, but i think it is a STDOUT.

[/quote]
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.

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

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:

[pre]

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

[/pre]

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.

[source code]

 

Good luck!

[quote quote=164620]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
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.6px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
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!

[/quote]
forgot to mention that the EXE I created was just a PowerShell script with [pre]Write-Host[/pre] in it, so I wasn’t redirecting anything in an effort to keep it simple.

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.

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"
}

 

 

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

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

 

Kind Regards,

Ben

No worries, glad we got you going.

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

https://www.adamtheautomator.com/powershell-start-process/