Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 04, 2016, 04:34:21 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: WinSendKeys (Automate Task Scheduler)  (Read 2795 times)

Day Agent

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
WinSendKeys (Automate Task Scheduler)
« on: November 09, 2012, 08:38:46 PM »
I have been trying to automate importing a task into Task Scheduler, to be run from a user account which has no password.
This requires [Enter] be pressed to confirm there is no password, before schtasks accepts the task.

A user account with a password would resolve the problem, but this is not an option.
NT AUTHORITY\SYSTEM would be accepted as a user without a password, but this is not an option either.

The syntax for importing the task (in my case) is this:

schtasks /create /xml "X:\task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"

To deal with the multiple quotes, the parameters are assigned to a variable PRMTRS.

Although schtasks %PRMTRS% works as expected, when I try to use WinSendKeys to launch schtasks with the parameters, wait a few seconds and then send [Enter], it doesn't work. This is (among several other variations) is the line I have tried:

WinSendKeys -x schtasks -xp %PRMTRS% -xd 5000 {ENTER}

I'd appreciate some advice.

"X:\task.xml", "COMPUTER NAME\USER NAME" and "Imported Task" are generic placeholders.
In reality, "COMPUTER NAME\USER NAME" might be "John's Computer\John Smith".
« Last Edit: November 10, 2012, 06:10:51 AM by Day Agent »

Day Agent

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #1 on: November 09, 2012, 09:59:51 PM »
WinSendKeys uses quotes to group input which includes spaces. As quotes are used in the same way by schtasks, I had hoped to avoid problems by assigning the parameters to be used by schtasks to a variable.

set PRMTRS=/create /xml "X:\task.xml" /ru "COMPUTER_NAME\USER NAME" /tn "Imported Task"

The debug log seems to indicate this did not work:

2012-11-10 04:20:06.131, Debug logging enabled
2012-11-10 04:20:06.136, Executable Filepath specified: schtasks
2012-11-10 04:20:06.140, Executable Parameters specified: /create
2012-11-10 04:20:06.144, exename: schtasks
2012-11-10 04:20:06.148, Window: /xml
2012-11-10 04:20:06.160, Windowhandle: 0
2012-11-10 04:20:06.164, Running executable: schtasks /create
2012-11-10 04:20:06.297, Running executable PID: 4048
2012-11-10 04:20:06.676, Window '' not found.

I tried  to group the entire set of parameters to be used by schtasks with quotes.

set PRMTRS="/create /xml "X:\task.xml" /ru "COMPUTER_NAME\USER NAME" /tn "Imported Task""

2012-11-10 04:24:08.292, Debug logging enabled
2012-11-10 04:24:08.297, Executable Filepath specified: schtasks
2012-11-10 04:24:08.301, Executable Parameters specified: /create /xml X:\task.xml
2012-11-10 04:24:08.307, exename: schtasks
2012-11-10 04:24:08.312, Window: /ru COMPUTER
2012-11-10 04:24:08.324, Windowhandle: 0
2012-11-10 04:24:08.329, Running executable: schtasks /create /xml X:\task.xml
2012-11-10 04:24:08.491, Running executable PID: 2204
2012-11-10 04:24:09.270, Window '' not found.

(The above is an approximation of the log, due to anonymization.)

It would seem that the quotes are causing the problem.

Ath, any chance of a quick fix?
A grouping symbol like ¶ (Right Alt + ; ) or × (Right Alt + =) would be less likely to conflict with parameters.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,778
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #2 on: November 10, 2012, 02:59:29 AM »
This is where the usual escape sequence for quotes is needed: Double the quotes

so I tried:
WinSendKeys.exe -d -xp "/create /xml ""X:\task.xml"" /ru ""COMPUTER NAME\USER NAME"" /tn ""Imported Task""" ?

and got in the log:
2012-11-10 09:55:34.931, Debug logging enabled
2012-11-10 09:55:34.932, Executable Parameters specified: /create /xml "X:\task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-10 09:55:34.932, Window: ?
2012-11-10 09:55:34.934, Windowhandle: 0
2012-11-10 09:55:34.934, Window '?' not found.


No special handling required I guess :D

Day Agent

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #3 on: November 10, 2012, 04:42:35 AM »
Ah )

WinSendKeys.exe -d -x schtasks -xp "/create /xml ""X:\task.xml"" /ru ""COMPUTER NAME\USER NAME"" /tn ""Imported Task""" {ENTER}

No more trouble with quotes.

Unfortunately, {ENTER} doesn't seem to get pressed. Is my syntax wrong?

2012-11-10 11:30:45.766, Debug logging enabled
2012-11-10 11:30:45.771, Executable Parameters specified: /create /xml "X:\task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-10 11:30:45.781, exename: schtasks
2012-11-10 11:30:45.785, Window: {ENTER}
2012-11-10 11:30:45.792, Windowhandle: 0
2012-11-10 11:30:45.796, Running executable: schtasks /create /xml "X:\task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-10 11:30:46.003, Running executable PID: 2244
2012-11-10 11:30:46.221, Window '' not found.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,778
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #4 on: November 10, 2012, 04:49:35 AM »
I think you also need to provide the name for the schtasks window that's active during the execution/wait for enter. It now is assuming it has to wait for a window called {ENTER}, and that's quite unlikely to exist. You may need a Window Spy tool or Process Explorer to find the exact name of the window/process while it is waiting for input.

Edit: Changing the -x parameter to -x schtasks.exe may be enough, but I have not enough test-data to try that out. Adding schtasks.exe before {ENTER} should fix it, AFAICS,
« Last Edit: November 10, 2012, 04:57:54 AM by Ath »

Day Agent

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #5 on: November 10, 2012, 05:55:55 AM »
Although the actual window title reads "C:\Windows\system32\schtasks.exe", the WindowName seems to be "schtasks.exe".

WinSendKeys -x schtasks -d -xp "/create /xml ""X:\task.xml"" /ru ""COMPUTER NAME\USER NAME"" /tn ""Imported Task""" schtasks.exe {ENTER}

2012-11-10 12:34:54.735, Debug logging enabled
2012-11-10 12:34:54.741, Executable Parameters specified: /create /xml "X:\task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-10 12:34:54.770, exename: schtasks
2012-11-10 12:34:54.774, Window: schtasks.exe
2012-11-10 12:34:54.783, Windowhandle: 0
2012-11-10 12:34:54.787, Running executable: schtasks /create /xml "X:\task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-10 12:34:54.948, Running executable PID: 3984
2012-11-10 12:34:55.122, Title for PID: 3984 hWND: 0x000A05BA / C:\Windows\system32\schtasks.exe
2012-11-10 12:34:55.133, Window 'C:\Windows\system32\schtasks.exe' activated.
2012-11-10 12:34:55.137, Send: {ENTER}

It works mostly but WinSendKeys can't always find the schtasks.exe window, possibly because it loads a little slow sometimes. Since a WinSendKeys Strokesfile can set WinSendKeys to wait for a window to appear, I put the following lines in a file and named it Wait.sro:

--winwait schtasks.exe 6
{ENTER}

WinSendKeys -x schtasks -xp "/create /xml ""X:\task.xml"" /ru ""COMP. NAME\USER NAME"" /tn ""Imported Task""" -f "X:\Wait.sro" schtasks.exe

Works like a charm now.

Nice program.  :)
« Last Edit: November 10, 2012, 11:35:56 PM by Day Agent »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,778
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #6 on: November 10, 2012, 06:15:56 AM »
Although the actual window title reads "C:\Windows\system32\schtasks.exe", the WindowName seems to be "schtasks.exe".
Well, the ProcessList() function of AutoIt returns just the name of the executable, and that is what is used/checked by WSK if a Window with the specified name can not be found.

Works like a charm now.

Nice program.  :)
Thanks :)
As usual, donations are welcome :-[ (not mandatory)

Day Agent

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #7 on: November 10, 2012, 11:04:46 PM »
Although 'Window '' not found' seem to happen more infrequently, it still happens if a Strokesfile is used.
This is not due to a time-out, however. The error is thrown immediately.

Quote
2012-11-11 05:45:37.110, Debug logging enabled
2012-11-11 05:45:37.117, Executable Filepath specified: schtasks
2012-11-11 05:45:37.122, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:45:37.128, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:45:37.134, exename: schtasks
2012-11-11 05:45:37.139, Window: schtasks.exe
2012-11-11 05:45:37.149, Windowhandle: 0
2012-11-11 05:45:37.154, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:45:37.308, Running executable PID: 2344
2012-11-11 05:45:37.772, Window '' not found.
2012-11-11 05:48:42.390, Debug logging enabled
2012-11-11 05:48:42.396, Executable Filepath specified: schtasks
2012-11-11 05:48:42.402, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:48:42.407, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:48:42.414, exename: schtasks
2012-11-11 05:48:42.419, Window: schtasks.exe
2012-11-11 05:48:42.429, Windowhandle: 0
2012-11-11 05:48:42.434, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:48:42.572, Running executable PID: 3668
2012-11-11 05:48:42.861, Window '' not found.
2012-11-11 05:50:06.660, Debug logging enabled
2012-11-11 05:50:06.667, Executable Filepath specified: schtasks
2012-11-11 05:50:06.672, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:06.689, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:50:06.695, exename: schtasks
2012-11-11 05:50:06.700, Window: schtasks.exe
2012-11-11 05:50:06.709, Windowhandle: 0
2012-11-11 05:50:06.714, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:06.879, Running executable PID: 3660
2012-11-11 05:50:07.002, Title for PID: 3660 hWND: 0x001B03CA / C:\Windows\system32\schtasks.exe
2012-11-11 05:50:07.039, Window 'C:\Windows\system32\schtasks.exe' activated.
2012-11-11 05:50:07.054, --winwait schtasks.exe maxwait: 6
2012-11-11 05:50:07.070, test exename: schtasks.exe
2012-11-11 05:50:07.287, Window: schtasks.exe
2012-11-11 05:50:07.793, Windowhandle: 0x001B03CA
2012-11-11 05:50:07.798, --winwait schtasks.exe waited: 0
2012-11-11 05:50:07.804, Send: {ENTER}
2012-11-11 05:50:07.922, Send:
2012-11-11 05:50:10.269, Debug logging enabled
2012-11-11 05:50:10.275, Executable Filepath specified: schtasks
2012-11-11 05:50:10.281, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:10.286, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:50:10.292, exename: schtasks
2012-11-11 05:50:10.298, Window: schtasks.exe
2012-11-11 05:50:10.307, Windowhandle: 0
2012-11-11 05:50:10.312, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:10.621, Running executable PID: 3472
2012-11-11 05:50:10.674, Title for PID: 3472 hWND: 0x002303DC / C:\Windows\system32\schtasks.exe
2012-11-11 05:50:10.690, Window 'C:\Windows\system32\schtasks.exe' activated.
2012-11-11 05:50:10.705, --winwait schtasks.exe maxwait: 6
2012-11-11 05:50:10.721, test exename: schtasks.exe
2012-11-11 05:50:10.913, Window: schtasks.exe
2012-11-11 05:50:11.420, Windowhandle: 0x002303DC
2012-11-11 05:50:11.426, --winwait schtasks.exe waited: 0
2012-11-11 05:50:11.432, Send: {ENTER}
2012-11-11 05:50:11.573, Send:
2012-11-11 05:50:13.363, Debug logging enabled
2012-11-11 05:50:13.369, Executable Filepath specified: schtasks
2012-11-11 05:50:13.376, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:13.383, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:50:13.390, exename: schtasks
2012-11-11 05:50:13.396, Window: schtasks.exe
2012-11-11 05:50:13.406, Windowhandle: 0
2012-11-11 05:50:13.416, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:13.567, Running executable PID: 2060
2012-11-11 05:50:13.672, Title for PID: 2060 hWND: 0x002403DC / C:\Windows\system32\schtasks.exe
2012-11-11 05:50:13.679, Window 'C:\Windows\system32\schtasks.exe' activated.
2012-11-11 05:50:13.688, --winwait schtasks.exe maxwait: 6
2012-11-11 05:50:13.695, test exename: schtasks.exe
2012-11-11 05:50:13.905, Window: schtasks.exe
2012-11-11 05:50:14.412, Windowhandle: 0x002403DC
2012-11-11 05:50:14.417, --winwait schtasks.exe waited: 0
2012-11-11 05:50:14.423, Send: {ENTER}
2012-11-11 05:50:14.721, Send:
2012-11-11 05:50:17.183, Debug logging enabled
2012-11-11 05:50:17.188, Executable Filepath specified: schtasks
2012-11-11 05:50:17.194, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:17.231, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:50:17.237, exename: schtasks
2012-11-11 05:50:17.241, Window: schtasks.exe
2012-11-11 05:50:17.313, Windowhandle: 0
2012-11-11 05:50:17.319, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:17.474, Running executable PID: 3036
2012-11-11 05:50:17.503, Title for PID: 3036 hWND: 0x000C0440 / C:\Windows\system32\schtasks.exe
2012-11-11 05:50:17.510, Window 'C:\Windows\system32\schtasks.exe' activated.
2012-11-11 05:50:17.519, --winwait schtasks.exe maxwait: 6
2012-11-11 05:50:17.526, test exename: schtasks.exe
2012-11-11 05:50:17.877, Window: schtasks.exe
2012-11-11 05:50:18.383, Windowhandle: 0x000C0440
2012-11-11 05:50:18.389, --winwait schtasks.exe waited: 0
2012-11-11 05:50:18.395, Send: {ENTER}
2012-11-11 05:50:18.518, Send:
2012-11-11 05:50:20.586, Debug logging enabled
2012-11-11 05:50:20.592, Executable Filepath specified: schtasks
2012-11-11 05:50:20.598, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:20.623, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:50:20.631, exename: schtasks
2012-11-11 05:50:20.637, Window: schtasks.exe
2012-11-11 05:50:20.702, Windowhandle: 0
2012-11-11 05:50:20.707, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:20.892, Running executable PID: 1316
2012-11-11 05:50:21.011, Title for PID: 1316 hWND: 0x000D0440 / C:\Windows\system32\schtasks.exe
2012-11-11 05:50:21.019, Window 'C:\Windows\system32\schtasks.exe' activated.
2012-11-11 05:50:21.027, --winwait schtasks.exe maxwait: 6
2012-11-11 05:50:21.036, test exename: schtasks.exe
2012-11-11 05:50:21.292, Window: schtasks.exe
2012-11-11 05:50:21.812, Windowhandle: 0x000D0440
2012-11-11 05:50:21.818, --winwait schtasks.exe waited: 0
2012-11-11 05:50:21.825, Send: {ENTER}
2012-11-11 05:50:22.141, Send:
2012-11-11 05:50:23.673, Debug logging enabled
2012-11-11 05:50:23.679, Executable Filepath specified: schtasks
2012-11-11 05:50:23.685, Executable Parameters specified: /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:23.691, Filemode, reading from file: X:\Wait.sro
2012-11-11 05:50:23.704, exename: schtasks
2012-11-11 05:50:23.710, Window: schtasks.exe
2012-11-11 05:50:23.727, Windowhandle: 0
2012-11-11 05:50:23.744, Running executable: schtasks /create /xml "X:\Task.xml" /ru "COMPUTER NAME\USER NAME" /tn "Imported Task"
2012-11-11 05:50:23.891, Running executable PID: 2484
2012-11-11 05:50:24.309, Window '' not found.
« Last Edit: November 10, 2012, 11:29:28 PM by Day Agent »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,778
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #8 on: November 11, 2012, 04:50:43 AM »
Can you attach your Strokesfile please?

Edit: Seeing that you (seem to have) added --winwait schtasks.exe 6 to wait a maximum of 6 seconds for the executable to be loaded, that is probably the solution you need. Running the executable without permitting it some time to get running is apparently a bit too quick.
That's not an error, a feature or a bug, but just how things go (in Windows), IMHO.
« Last Edit: November 11, 2012, 05:29:07 AM by Ath, Reason: After analysis of WSK source... »

Day Agent

  • Supporting Member
  • Joined in 2012
  • **
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: WinSendKeys (Automate Task Scheduler)
« Reply #9 on: November 11, 2012, 05:06:00 PM »
The Strokesfile is very basic:

--winwait schtasks.exe 6
{ENTER}


I tried:

--winwait schtasks.exe
{ENTER}


but the result(ing logfile) is the same.

The 'Window '' not found' error is thrown immediately, whether waiting for 6 seconds or not. Wouldn't the logfile show a time-out error if that were the problem?

This is what happens:

  • I open an elevated command prompt and enter: WinSendKeys -x schtasks -xp "/create /xml ""X:\task.xml"" /ru ""COMP. NAME\USER NAME"" /tn ""Imported Task""" -f "X:\Wait.sro" schtasks.exe
  • When I hit [Enter], almost immediately (-1 second) a new command prompt window opens, which states "Please enter the run as password for COMPUTER NAME\USER NAME:"
  • - When things work as intended, WinSendKeys sends am [Enter] and the window closes.
  • - When they don't, almost at the same moment the new command prompt opens (so fast, it's hard to tell which is first) a window pops-up with the 'Window '' not found' error.

I have noticed that if I repeatedly start WinSendKeys (with my line), it will error from the first try and keep on erroring until it works. Then it will work until it errors. It will usually error only once and then work again a number of times in a row, etc.

« Last Edit: November 11, 2012, 05:23:35 PM by Day Agent »