0
votes

I have set of windows virtual machines, where I'm running a task every minute using task scheduler (schtasks.exe)

There is one batch file which creates the task, looks something like this:

SCHTASKS.EXE /Delete /TN "my_server" /F

SCHTASKS.EXE /CREATE /SC MINUTE /MO 1 /TN "my_server" /TR X:\mydrive\server.bat /RU %computername%\<username>

SCHTASKS.EXE /RUN /TN "my_server"

So this task runs every minute and every time it opens a command prompt window.

I want this task to run every minute but not open command prompt every time - it should run in minimized mode.

After googling for a solution, I've found few answers on stackoverflow itself to try /RU "NT Authority\System" instead of current user. So I've tried this as well:

SCHTASKS.EXE /CREATE /SC MINUTE /MO 1 /TN "my_server" /TR X:\mydrive\server.bat /RU "NT AUTHORITY\SYSTEM"

But it didn't work for me, not sure why. It didn't report any errors, I could see on command prompt "Task has been successfully created, Attempted to run the task", it scheduled the task but didn't start the task.

1
try changing /TR X:\mydrive\server.bat by /TR start /min "" cmd /C "X:\mydrive\server.bat" or open the task in the management console and check run whether the user is logged or notelzooilogico
@elzooilogico I tried this : SCHTASKS.EXE /CREATE /SC MINUTE /MO 1 /TN "my_server" /TR start /min "" cmd /C "X:\mydrive\server.bat" /RU %computername%\<username> and got this error : ERROR: Invalid argument/option - '/min'. Type "SCHTASKS /CREATE /?" for usage.Ayush
/TR "start /min "" cmd /C "X:\mydrive\server.bat"" or /TR "start /min cmd /C ^"X:\mydrive\server.bat^"" or /TR "start /min cmd /C \"X:\mydrive\server.bat\""elzooilogico
Both reported same error - ERROR: Invalid argument/option - 'cmd'. SCHTASKS.EXE /CREATE /SC MINUTE /MO 1 /TN "my_server" /TR "start /min "" cmd /C "X:\mydrive\server.bat"" /RU %computername%\<username> and your 2nd method SCHTASKS.EXE /CREATE /SC MINUTE /MO 1 /TN "my_server" /TR "start /min ^"^" cmd /C ^"X:\mydrive\server.bat^"" /RU %computername%\<username>Ayush
/TR "start /min ""cmd /C "X:\mydrive\server.bat""" if this doesn' work, I suggest to create the task as you were originally doing, but place in server.bat the line. start /min "" cmd /c "X:\mydrive\server1.bat" and write your actual code in the new fileelzooilogico

1 Answers

0
votes

I don't think you can accomplish this under Scheduled Tasks. There are, however, other ways.

One way is to create an executable (exe) program that runs the batch file as a child process and then have the exe either have no window or position the window off of the screen. This works similar to creating a shortcut to the batch file and changing the Window Position properties of the shortcut under the Layout tab to be 5000, 5000. That sort of works but it does put an icon in the task bar that disappears after it is run. Unfortunately you can't run that shortcut from a scheduled task. When attempting to run batch files from the task manager be aware that the task is running as a child process of taskeng (task engine) and it is that process that controls the cmd.exe instance. I don't know for a fact, but I suspect that taskeng reads the shortcut and executes its contents rather than having cmd handle the shortcut directly.

If you have a compiler of some sort you can create an exe that runs the batch file in a hidden or minimized window using the Windows shell. With a minimized window you will still get the task bar icon. With a hidden window you get neither the cmd window nor the task bar icon. I've also heard you can do this with a VBS script also.

I had one job where the client wanted to create the batch file on the fly, run it, and immediately delete the batch file so it couldn't be tampered with by users. You can also do that with an exe.

There is also a way to do it with INNO Setup which makes installation programs. You include the batch file in the installation, run it, and then delete the batch file. INNO has a command line switch /VeryQuiet that can be used with the exe produced to hide the user interface window. This can be used in a scheduled task.

A couple of notes about Scheduled Tasks under Vista and later: If you choose "Run whether or not a user is logged on" the task will use S4U authentication so your batch file won't have access to any network resources like mapped drives or UNC paths. And if you use select "Use highest privileges" it will run under the built in Administrator account that is created when Windows is installed. This does not elevate the given user to have administrator group privileges but is an actual separate user account with a separate security token. As such, it won't see any of the user's resources like mapped drives and it too will use S4U autehntication so won't have other network access either.

There is also the Windows AT.exe utility. Type AT /? at a command prompt. The AT command sets up a task with no user interface. The only way to see if it runs at the given time is through Task Manager because there is no window or task bar icon. The downside is that it only records one task per day. So you would have to set up a way to create a batch file to create the task for each minute of the day for the period you want it to run, e.g.

AT 08:00 SomeBatchFile.bat
AT 08:01 SomeBatchFile.bat
AT 08:02 SomeBatchFile.bat
.
.
.

I'm pretty sure it requires a logged in user so it can't be run when no one is logged in. I did not, however, test this. It wasn't clear from your post what security you needed for the scheduled task. Running under a logged in user will also enable network resource access.

I did test whether or not the tasks survive a re-boot. They do, so they will be present after a restart. You can only get rid of them with

AT <Task#> /DELETE

I don't know if they will work in virtual machines.

I have no idea what resources are used by the AT command but it might bog the system down if you were to run 1440 instances for 24 hours or 720 instances for 12 hours.