• Announcements

Sign in to follow this  
Followers 0

Taskplaner/Taskscheduler COM UDF

47 posts in this topic

Posted (edited) · Report post

Hi all,

i needed an UDF for using the Windows Taskplaner / Task Scheduler.

Everything i found was

  • based on at.exe (outdated) or
  • based on schtasks.exe (limited in functionality) or
  • using WMI (which can not see task that were not created with WMI and is also limited)

The only good solution i found was here: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517

But it only had a Create Function.

With my limited knowledge i tried creating more functions and it seems to work quite OK.

You can try it out with TestTaskSchedulerUDF(). WinVista or higher is required.

All feedback is welcome, but please be kind, i do not have much practise and i am well aware that there is room for improvment.

Edit: added new version of the code with more functions:
Edit2: updated UDF fixed bugs
Edit3: added info for multiinst parameter (_TaskCreate)
Edit4: updated UDF (some bugfixes)
Edit5: added version info to UDF, added changes/additions suggested in this thread (_TaskIsEnabled(), _TaskEnable(), _TaskDisable() and $duration for _TaskCreate())
Edit6: added new version that does not use a COM errorhandler (this version can be mixed with other com functions with or without errorhandler, but requires current autoit beta 3.3.9.4 or newer) - see this post for details: http://www.autoitscript.com/forum/topic/138279-com-error-handler-return-values/#entry968768 (if you do not need other COM routines, there is no need to change from the last version to this one, but all newer version will be without errorhandler...)
Edit7: added version 5.2 with new TaskCreate option $starwhenavailable

Edit8: added version 5.3 fixed versioncompare problems and some typos (now runs with latest beta 3.3.9.22)

As long as there is no feedback this will be the last update...for me it works like it should and i can manage all my tasks just fine!
Keep in Mind that Autoit does not support using more than one ComError Function so be careful if you use other com udfs that need a com errorfunction - this is not a problem when using the taskplanerCOMneh.au3 version (neh=no error handler)


Now we have:

  • Func _TestTaskSchedulerUDF()
  • Func _TaskSchedulerAutostart();check if the schedulerservice is set to autostart
  • Func _TaskIsValidPlatfrom() ;check if os and autoit versions are OK for use with this udf
  • Func _TaskFolderCreate($folder);Create folder - only folders that do not already exist can be created without error
  • Func _TaskFolderDelete($folder);Delete folder - only folders that do exist and that are empty can be deleted without error
  • Func _TaskFolderExists($folder = "");check if a TaskFolder exists
  • Func _TaskExists($taskname, $folder = "");check if a Task exists
  • Func _TaskStop($taskname, $folder = "");stop a task
  • Func _TaskStart($taskname, $folder = "");start a task
  • Func _TaskIsRunning($taskname, $folder = "");check if a Task is running
  • Func _TaskIsEnabled($taskname, $folder = "");check if a Task is enabled
  • Func _TaskEnable($taskname, $folder = "");Enable Task
  • Func _TaskDisable($taskname, $folder = "");Disable Task
  • Func _TaskDelete($taskname, $folder = "");delete an existing task
  • Func _TaskListall($folder = "", $hidden = 1);get a list of all tasks in a given taskfolder (Tasknames only) List is a string with names separeted by "|"
  • Func _TaskCreate($taskname, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $LogonType, $RunLevel, $Username, $Password, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True, $active = True, $multiinst = 0, $nobatstart = False, $stoponBat = False, $hidden = False, $idle = False, $WakeToRun = False, $timelimit = "P1D", $priority = 5, $duration="", $StartWhenAvailable = True)
Edited by Allow2010
mLipok, AvvA and ModemJunki like this

Share this post


Link to post
Share on other sites



Posted · Report post

This topic should probably be in the Example Scripts forum, not the ActiveX/COM Help and Support forum.

P.S. I don't have vista so I can't test it :)

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

as this is very fresh and i could very well need some help to improve it, i think it is no yet ready to be an example-)

>WinVista or higher is required.
>>P.S. I don't have vista so I can't test it

hmm, but you probaly have something newer than Vista? Or are you still running W2000/9x? Edited by Allow2010

Share this post


Link to post
Share on other sites

Posted · Report post

hmm, but you probaly have something newer than Vista? Or are you still running W2000/9x?


Nope. :) WinXP

Share this post


Link to post
Share on other sites

Posted · Report post

:-) Forgot about XP:-)

Well the COM based method is (IMHO) the best solution as long as no os support below Vista is needed.

I could not find a solution that offers the same possibillities on all OS version.

Share this post


Link to post
Share on other sites

Posted · Report post

Update: added more functions to first post

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

as i noticed the code is a bit stupid:-)

If one function fails, every function returns a failure...will fix that and post a new version... Edited by Allow2010

Share this post


Link to post
Share on other sites

Posted · Report post

This page here http://msdn.microsoft.com/en-us/library/windows/desktop/aa381341%28v=VS.85%29.aspx
tells me that i can get a returncode

Return value

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

but $result = $oFolder.CreateFolder($folder) seems to return
[out] ITaskFolder **ppFolder
so any idea how i can get the return code? Thanks !

Share this post


Link to post
Share on other sites

Posted · Report post

updates udf in first post.

fixed bugs, changed some names and comments

my question above is still open...help from some com professional is welcome.-)

Share this post


Link to post
Share on other sites

Posted · Report post

Updated first post with some info and updated UDF (changed parameter help only)

Share this post


Link to post
Share on other sites

Posted · Report post

Looks really nice :)! And yeah, maybe a Mod better moves this to the example forum.

Share this post


Link to post
Share on other sites

Posted · Report post

Allow2010,

Do you want it moved?

M23

Share this post


Link to post
Share on other sites

Posted · Report post

as i am not sure if it is good enough to be an example, its up to you.-)

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

Allow2010,

You will get many more hits here - could help you make it even better. :)

M23 Edited by Melba23
Wrong button too soon!

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

added new version to first post with some bugfixes/changes for latest AutoIt version Edited by Allow2010

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

I am impressed with your UDF and I think it is much better than schtasks.exe

I want to create a daily task, which
  • starts at 6:30
  • repeats every 6 hours, for a duration of one day
At the moment I can create a daily task which starts at 6:30, repeats every 6 hours but indefinately. I would like to change that to a duration of one day if possible?

_TaskCreate("Smart-Net\SmartRAS Monitor Script", "Report in info about the Smart-RAS to SNID", 2, "2000-01-01T06:30:00", "", "", "", "", "", 1, "PT6H", True, 1, 0, $username,$password, """"&$app&"""", "", "", False, True,0,True,True,False,False,False,"P3D",5)


EDIT

Have modified the UDF to support $Duration

 
 If $RepetitionEnabled Then
  $oTriggerRepetition = $oTrigger.Repetition()
  $oTriggerRepetition.Interval() = $Interval
  $oTriggerRepetition.Duration() = $Duration
 EndIf


Now I am running this code to do what I need

_TaskCreate("Smart-Net\SmartRAS Monitor Script", "Report in info about the Smart-RAS to SNID", 2, "2000-01-01T06:30:00", "", "", "", "", "", 1, "PT6H","P1D", True, 1, 0, $username,$password, """"&$app&"""", "", "", False, True,0,True,True,False,False,False,"P3D",5)


Thanks for the code, its extremely useful. BTW if others want to modify the code I recommend using powershell script here to enumerate the schedule task objects Edited by NDog

Share this post


Link to post
Share on other sites

Posted · Report post

thanks...good addition...

Share this post


Link to post
Share on other sites

Posted · Report post

Looks like a useful UDF Allow2010. Thanks ;)

A very small suggestion: The _TaskCreate function has a dayof month parameter which has to be some power of 2. It would be easier (for me at least) if this parameter were just the actual day of the month. To keep it compatible with previous versions you could say that if a negative value is used it means the actual day number (1 - 31). Use -32 for "last day". Then in the function you could have
if $DaysOfMonth < 0 then
 $DaysOfMonth = 2^(-$DaysOfMonth - 1)

Share this post


Link to post
Share on other sites

Posted (edited) · Report post

Just came across this today and have to say very impressive. Thanks for posting it. There is room for improvement e.g. _TaskSchedulerAutostart() can be consolidated into one line or three if you count Func and EndFunc.

Func _TaskSchedulerAutostart()
	Return RegRead("HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesSchedule", "Start") > 0 ; Returns True or False.
EndFunc   ;==>_TaskSchedulerAutostart
Edited by guinness

Share this post


Link to post
Share on other sites

Posted · Report post

Thanks for this, I have been using it successfully wish a few more options were there tho

Share this post


Link to post
Share on other sites

Posted · Report post

thank you for the script Allow2010 .

i working on a project these day , who basically disable unwanted things loaded on startup , especially updaters .

i solved my needs for windows xp using the old microsoft jt.exe (referring here only for the scheduled tasks part)
but , in windows seven my script dont work properly.

i try to change/complete your script , but probaly i do not find/apply the right methods and/or properties .
i cant find much info on the net for theese .

if dont want bother yoy , but some new functions like
  • Func _TaskEnable($taskname, $folder = ""); enable a Task
  • Func _TaskDisable($taskname, $folder = ""); disable a Task
  • Func _TaskCheckIsEnabled($taskname, $folder = "");check if a Task is enabled
can help me and other when i finish/publish my project

i suppose all 3 function will set/check

$oSettings.Enabled() = $active ;True ; The task can be performed only when this setting is True.

.

thank you !

oneLess

Share this post


Link to post
Share on other sites

Posted · Report post

  • Func _TaskEnable($taskname, $folder = ""); enable a Task
  • Func _TaskDisable($taskname, $folder = ""); disable a Task
  • Func _TaskCheckIsEnabled($taskname, $folder = "");check if a Task is enabled

i found the right property here : http://msdn.microsoft.com/en-us/library/windows/desktop/aa380757(v=vs.85).aspx

$oTask.Enabled = False ; [= 0 , will disable the task]
$oTask.Enabled = True ; [=-1 , will enable the task]
$_a = $oTask.Enabled ; [will read the status of the task , 0=False=Disabled , -1=True=Enabled]

more options in reading of status here : http://msdn.microsoft.com/en-us/library/windows/desktop/aa380770(v=vs.85).aspx
$oTask.State is the property ; [TASK_STATE_DISABLED = 1 = Disabled]


oneLess

Share this post


Link to post
Share on other sites

Posted · Report post

after a long time i checked this tread and i thank everybody for the comments and ideas...

I will try to update the UDF and if someone has done some of those modifications already or has other useful hints, please post here soon, so i do not have to do something that is already done:-))

Share this post


Link to post
Share on other sites

Posted · Report post

Looks like a useful UDF Allow2010. Thanks ;)

A very small suggestion: The _TaskCreate function has a dayof month parameter which has to be some power of 2. It would be easier (for me at least) if this parameter were just the actual day of the month. To keep it compatible with previous versions you could say that if a negative value is used it means the actual day number (1 - 31). Use -32 for "last day". Then in the function you could have

if $DaysOfMonth < 0 then
$DaysOfMonth = 2^(-$DaysOfMonth - 1)

maybe i get it wrong:

when i want to set day 10 and day 11 for a task to run i will combine 512+1024=1536
If i use real day numbers like -10 + -11 = -21 and then do

$DaysOfMonth = 2^(-$DaysOfMonth - 1)
2^(- (-21) -1)= 2^20= 1048576

this will not work?!

Share this post


Link to post
Share on other sites

Posted · Report post

I am impressed with your UDF and I think it is much better than schtasks.exe

I want to create a daily task, which

  • starts at 6:30
  • repeats every 6 hours, for a duration of one day
At the moment I can create a daily task which starts at 6:30, repeats every 6 hours but indefinately. I would like to change that to a duration of one day if possible?

_TaskCreate("Smart-NetSmartRAS Monitor Script", "Report in info about the Smart-RAS to SNID", 2, "2000-01-01T06:30:00", "", "", "", "", "", 1, "PT6H", True, 1, 0, $username,$password, """"&$app&"""", "", "", False, True,0,True,True,False,False,False,"P3D",5)


EDIT

Have modified the UDF to support $Duration

If $RepetitionEnabled Then
$oTriggerRepetition = $oTrigger.Repetition()
$oTriggerRepetition.Interval() = $Interval
$oTriggerRepetition.Duration() = $Duration
EndIf


Now I am running this code to do what I need

_TaskCreate("Smart-NetSmartRAS Monitor Script", "Report in info about the Smart-RAS to SNID", 2, "2000-01-01T06:30:00", "", "", "", "", "", 1, "PT6H","P1D", True, 1, 0, $username,$password, """"&$app&"""", "", "", False, True,0,True,True,False,False,False,"P3D",5)


Thanks for the code, its extremely useful. BTW if others want to modify the code I recommend using powershell script here to enumerate the schedule task objects

i implented you changes, but duration was added to the end of the paramter list for taskcreate in order to not break compatibillity...

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0