How to get Current Stdout from process - powershell

I am trying to write a PowerShell script that will run a console app which is a .exe file, poll the stdout from the .exe, and depending on what get's printed to the console, will invoke a key-stroke or exit (Ctrl+C) the application. I want to be able to read the console output while the process is still running. So I don't want to wait until the process is complete.
Initially, I tried to accomplish this by creating a System.Diagnostics.Process object and calling .Start() on that process and then getting the .StandardOutput from that object. But that looks like it waits until the process is complete as I have tested the ping command.

Related

Run synchronous powershell script on startup

I'm aiming to run a Powershell script on the startup on my windows 10. This tutorial has perfectly explained that: https://devblogs.microsoft.com/scripting/use-powershell-to-create-job-that-runs-at-startup/
But I expect the job to keep my script running as my app is listening to some events but when I check the status of the job using the Get-job cmdlet it shows that it's completed. I think that it's treating my script as an asynchronous one and eventually, my doesn't listen to anything. Any idea how can I make it synchronous i.e. keep my script running forever? This is the content of my ps1 file:
C:\Users\m\Desktop\Scripts\Activate.ps1
python C:\Users\m\Desktop\app.py

Silent bat file execute powershell command

We have an application server running as a service, when some configuration is loaded it starts a bat script which has to run the powershell command Stop-ClusterGroup DRMSERVICES and then start it again.
The bat file works flawless when I manually execute it by dobbelt clicking. But when the service is running the bat, it does not finish, or execute the powershell command.
Bat file looks as follows
#echo off
powershell -command Stop-ClusterGroup DRMSERVICES
powershell -command Start-ClusterGroup DRMSERVICES
The service runs the bat file in silent mode, as a main difference.
I have tried with various switches including the -ExecutionPolicy Unrestricted and START /wait etc
Creating a seperate ps1 file and have the bat execute this instead.
All with the same output:
Manually executing the bat works
When the service executes the bat, it does not work.
I know the bat file is executed by the service, as inserting NET STOP servicename is working correct.
In the powershell event viewer I can also see event of the powershell commands take place.
The difference between manually executing and have the service execute the command in the event viewer, is event id 800 which states info about 'execution pipe' this is not present when the service is executing the bat.
The service does not wait for the powershell, and thus it does not have time to stop the cluster before exiting.
I'm lost whether this is a permission issue, syntax error or whatever.
Hopefully somebody can help
UPDATE:
I have tried with all proposed solutions, all with same result, the bat file works when double clicked, but the service does not execute the powershell command. Pure cmd is executed, as I can pipe to a txt file. I even got to a point when trying runas that the output log text wrote "insert administrator password"
I even managed to have our software guy change our software to call a powershell directly instead of a bat, same result. Powershell won't execute the command, this tells me it probably is permission, but everything have been set to log in as admin and run as admin for the sake of success, but still nothing.
I solved the problem.
Because the service is a 32bit process, it will execute a 32bit powershell.
FailoverClusters module only exists as a 64bit module.
By using %SystemRoot%\sysnative\WindowsPowershell\v1.0\powershell.exe
The service is able to open a 64bit session, and thus use the failover cluster module.
As a side note, the sysnative folder is only visible from a 32bit session, therefore it cannot be found via browsing in a 64bit os.
I think i have dealt with this kind of issue before, after the,
powershell -command Stop-ClusterGroup DRMSERVICES
you need to have cmd wait for a certain number of seconds, and then test if the DRMSERVICES is now stopped, if it is stopped then to start the DRMSERVICES again. This way cmd will keep waiting, and then check if the service has stopped.
After a certain number of tries, maybe have a way to stop checking and exit the script, for example it is trying to stop the service, and has run into a problem.
There is a timeout command in cmd

Typing "exit" closes foo.exe along with powershell

I set the path to a program, say "foo.exe", to my system path and so typing foo in cmd/powershell starts the program. However when I type exit to get out of cmd/powershell foo.exe also closes with it. Why does this happen and how do I prevent this from happening?
This doesn't happen for all programs, only certain ones which means those certain ones should be added to path in a different way probably or should be started in a different way I'm guessing. However, searching over the internet for a long time didn't give me anything so a little help would be appreciated.
If foo.exe is a console application (one compiled for the Windows console subsystem), it will run synchronously in cmd.exe / PowerShell: that is, control won't be returned to the calling shell until the application exits. This means that you won't even get a chance to type exit until foo.exe has already exited.
However, it is possible to run a console application asynchronously, namely if you use a job to run it, via Start-Job or Start-ThreadJob; that is, foo.exe will then run in the background.
In that event, exiting the calling shell with exit will terminate the foo.exe process.
To prevent that, you can use the Start-Process cmdlet instead; on Windows, you can use it to launch foo.exe directly, which will open in a new console window by default; on Unix-like platforms, you must launch it via the nohup utility (which sends the program's output to a file named nohup.out in the current directory).
By contrast, if foo.exe is a GUI-subsystem application, it launches asynchronously and independently of the calling shell: that is, control returns to the calling shell right after successful creation of the new process, and exiting the shell has no effect on that new process.

Batch Script not Releasing after Execution

I have a batch script which performs file version controlling after a Backup event has taken place. This batch script, writing to a normal txt logfile, calls a PowerShell script to send this Logfile as an attachment with a success notification email. I have managed to release the writing lock on the log file, to allow PS to attach and send the file, but the Batch script does not stop after the entire sequence has been completed.
When I check the log file, I see that the shell instance has placed a 'Pause' in the script, instead of a self-termination (as it is instructed), and results in:
Press any key to continue... with a waiting shell
an app locked logfile, which won't allow the script to run again, unless the logfile is released.
This is the sequence of events:
The only Pause I have, is in < bak_send_exec.bat > - its sole purpose is to start a PS script:
PowerShell.exe -noprofile -executionpolicy bypass
If I remove it, the PS does not start. If I have it in there, the PS starts and executes flawlessly, but the logfile stays locked in a shell instances which is in Paused state, until someone kills the cmd.exe instances which locked the file.
This runs on a weekend at 01:00 am, so user intervention should not be required.
VC Script Summary:
This inter-connected batch files renames two identical files (in different locations) with timestamps. The timestamp is written to a variable for use in a notification email, which is sent using a PowerShell command. The entire process is logged to a txt log file (file overwritten when script runs again), and the log file is included with a Notification Email, mentioned earlier.
Script Calls:
Initial Start Command: Triggers the Version Control Procedures and Logs Progress with versioncontrol_post.bat > TSLog.txt 2>&1
versioncontrol_post.bat: Performs main procedure, then ends with CALL bak_send_exec.bat
bak_send_exec.bat: The suspected cause... Coding of entire file is three lines long, but required as mentioned earlier, for policy relaxation:
#ECHO OFF
PowerShell.exe -noprofile -executionpolicy bypass -file bak_send.ps1
PAUSE
bak_send.ps1: Performs main procedure to make a copy of the temporary log (TSLog.txt) to its final home, releases the TSLog file to work with the new duplicate of it, and continues to take that new duplicate and attach it to an email and sends email. The final line in the procedure is EXIT.
Fault finding tells me that the issue is not with the PowerShell script, but rather with the script that calls it. Taking out the PAUSE command results in the PowerShell not starting.
Does anyone have a possible solution to this "feature"?

Returning handle to calling script perl

I have an executable which can run perl scripts using the following command at the prompt:
blah.exe Launch.pl
The way we have our tests setup is that we call the Launch.pl from Parent.pl like this "blah.exe Launch.pl" - script within script. However, when executing the command with backticks/system command the parent .pl script execution waits till I get the handle back by closing and exiting out of the application (blah.exe). At this point the code in parent.pl continues to execute.
How do I return the handle back to the parent .pl script after I get done running the code that is contained in the Launch.pl
So, parent.pl calls "blah.exe Launch.pl"; but after running the code inside Launch.pl inside the application (blah.exe) it just sits there waiting to be exited out of so that the code in parent.pl can continue running. I need to keep the application (blah.exe) open till I am done running a bunch of scripts one after another.
Run blah.exe in the background. When you are done with the Parent.pl, terminate the application with kill.