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?

geercom 76 Reputation points
2022-05-27T15:48:53.05+00:00

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
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,687 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 32,076 Reputation points
    2022-05-28T13:24:22.813+00:00

    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
    
    0 comments No comments