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?
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.exe2>&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?
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.
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.
[/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.