question

geercom-4104 avatar image
0 Votes"
geercom-4104 asked MotoX80 answered

How do you set a Scheduled Task to deal with a Start button and Exit button that the task must press to start and stop the .exe?

I scheduled a Task in Task Scheduler. However, the application I want to run has a Start button that I must click manually and an Exit button I must click manually, otherwise it won't run and it won't exit the program when it is done.

As of now, all the Task Scheduler is able to do is open the .exe file, which I find open the next morning. I have to click Start myself to run it, and click Exit myself to exit when it completes.

How do I get the Task to automatically click the Start button to run the program? How do I get it to automatically click the Exit button when it is done running? Are there some command switches or other entries I must include when setting up the Task in Task Scheduler? Exactly where and how do I add them to make it work as I describe? If that's not it, how do I make this work?

windows-10-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

Any command line switches would be application specific, so you would need to search the documentation for that program or contact the vendor's support team.

The best solution is to ask the vendor if they have a command line version of that program. Running GUI's in task scheduler can be problematic because they were developed to interact with a user as opposed to a background/batch type process.

Having said that, with Powershell you can try using SendKeys to mimic keystrokes. The problem is that you can't "read the screen". So you would need to add a sleep cmdlet for some time to let the program execute.

https://newbedev.com/sendkeys-method-in-powershell

https://www.dorkbrain.com/docs/2017/09/02/sendkeys-in-powershell/

Here is a sample script.

 function wait {
   param([int]$stop = 3)
   Start-Sleep -seconds $stop
 }
    
 function SendWait {
   param([string]$keys)
   [System.Windows.Forms.SendKeys]::SendWait($keys) 
 }
    
    
 add-type -AssemblyName microsoft.VisualBasic
 add-type -AssemblyName System.Windows.Forms
    
 $rdp = Start-Process mstsc.exe -PassThru                         # launch a GUI 
 wait
 [Microsoft.VisualBasic.Interaction]::AppActivate($rdp.ID)
 SendWait("ABCDEFGHIJKLM")                                       # type in a machine name
 wait 
 SendWait("{TAB}{TAB}")                                          # tab to the connect button 
 wait 
 SendWait("{ENTER}")                                            # try to connect 
 wait 5
 SendWait("{ENTER}")                                            # close the popup 
 wait
 SendWait("%{F4}")                                             # close the program alt+F4


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.