Sign in to follow this  
Followers 0
dbzfanatic

Task Scheduler UDF

39 posts in this topic

#1 ·  Posted (edited)

Well I saw that someone wanted a UDF for the windows Task Scheduler so I decided to try my hand at it.

#cs ----------------------------------------------------------------------------
    
    AutoIt Version: 3.2.13.7 (beta)
    Author:      dbzfanatic
    
    Script Function:
    Task Scheduler UDF (Task.au3)
    
#ce ----------------------------------------------------------------------------

Global $sRet

; #FUNCTION#;===============================================================================
;
; Name...........: _TaskSchedule
; Description ...: Adds a scheduled task.
; Syntax.........: _TaskSchedule($sDay, $sTime, $hProgram, $sName, $iID, $bInteractive, $iOccurrence)
; Parameters ....: $sDay - The day(s) to run the task. (ex. M, T, Th; 1, 2, 3, 15, 22)
;                   $sTime - The time to run the program.
;                   $hProgram - Path to the program/batch file to run.
;                   $sName - [Optional] The name of the computer on which to schedule the task.
;                   $iID - [Optional] The id to give the specified task.
;                   $bInteractive - [Optional] Boolean value to allow interaction with the desktop
;                   $iOccurrence - [Optional] Determines how often the program/batch file is run.
;~                      |1 - Every day
;~                      |2 - Once
; Return values .: Success - Deletes the specified task.
;                 Failure - Sets @Error:
;                 |1 - Invalid $sName
;                 |2 - Invalid $sTime
;                 |3 - Invalid $iID
;                 |4 - Invalid $bInteractive
;                 |5, 7 - Invalid $iOccurrence
;                 |6 - Invalid $hProgram
;                 |16 - Command failed
; Author ........: Michael Duane LaBruyere (dbzfanatic)
; Modified.......:
; Remarks .......:
; Related .......: _TaskDelete(), _TaskGetSchedule()
; Link ..........;
; Example .......; Yes
;
;;==========================================================================================

Func _TaskSchedule($sDay, $sTime, $hProgram, $sName = "", $iID = 1, $bInteractive = True, $iOccurrence = 1)
    
    Local $sInteract, $sOccur, $hRun
    
    If Not IsString($sName) Then
        Return SetError(1)
    EndIf
    
    If Not IsString($sTime) Then
        Return SetError(2)
    EndIf
    
    If Not IsInt($iID) Then
        Return SetError(3)
    EndIf
    
    If Not IsBool($bInteractive) Then
        Return SetError(4)
    EndIf
    
    If Not IsInt($iOccurrence) Then
        Return SetError(5)
    EndIf
    
    If $hProgram = "" Then
        Return SetError(6)
    EndIf
    
    If $bInteractive = True Then
        $sInteract = "/interactive"
    Else
        $sInteract = ""
    EndIf
    
    If $iOccurrence = 1 Then
        $sOccur = "/every:" & $sDay
    ElseIf $iOccurrence = 2 Then
        $sOccur = "/next:" & $sDay
    Else
        Return SetError(7)
    EndIf
    
    $hRun = Run("cmd.exe /c at " & $sName & " " & $iID & " " & " " & $sDay & " " & $sInteract & " " & $sOccur & " " & $hProgram, @SystemDir, @SW_HIDE)
    
    While ProcessExists("cmd.exe")
        $sRet = StdoutRead($hRun, True)
    WEnd
    If $hRun = 0 Then
        Return SetError(16)
    EndIf
    Return $sRet
    
EndFunc;==>_TaskSchedule

; #FUNCTION#;===============================================================================
;
; Name...........: _TaskGetScheduled
; Description ...: Returns a list of scheduled tasks.
; Syntax.........: _TaskGetScheduled()
; Parameters ....: 
; Return values .: Success - Returns the list of scheduled tasks.
;                 Failure - Sets @Error:
;                 |1 - Command Failed
; Author ........: Michael Duane LaBruyere (dbzfanatic)
; Modified.......:
; Remarks .......:
; Related .......: _TaskSchedule(), _TaskDelete()
; Link ..........;
; Example .......; Yes
;
;;==========================================================================================
Func _TaskGetScheduled()
    
    Local $hAt
    
    $hAt = Run("cmd.exe /c at", @SystemDir, @SW_HIDE, 8)
    While ProcessExists("cmd.exe")
        $sRet = StdoutRead($hAt, True)
    WEnd
    If $hAt = 0 Then
        Return SetError(1)
    EndIf
    Return $sRet
EndFunc;==>_TaskGetScheduled

; #FUNCTION#;===============================================================================
;
; Name...........: _TaskDelete
; Description ...: Deletes a scheduled task.
; Syntax.........: _TaskDelete($sName, $iID, $iDelete)
; Parameters ....: $sName - The name of the computer to execute the deletion on.
;                   $iID - [Optional] The id of the task to delete. If blank will delete the first scheduled task.
;                   $iDelete - [Optional] Integer value to determine deletion type. If 0 will delete all tasks, 1 will delete on the specified task.
; Return values .: Success - Deletes the specified task.
;                 Failure - Sets @Error:
;                 |1 - Invalid $iDelete
;                 |2 - Invalid $iID
;                 |3 - Invalid $sName
;                 |4 - Command Failed
; Author ........: Michael Duane LaBruyere (dbzfanatic)
; Modified.......:
; Remarks .......:
; Related .......: _TaskSchedule(), _TaskGetSchedule()
; Link ..........;
; Example .......; Yes
;
;;==========================================================================================
Func _TaskDelete($sName, $iID = 1, $iDelete = 1)
    
    Local $hDel, $sDelete
    
    If Not IsInt($iDelete) Then
        Return SetError(1)
    EndIf
    
    If Not IsInt($iID) Then
        Return SetError(2)
    EndIf
    
    If Not IsString($sName) Then
        Return SetError(3)
    EndIf
    
    If $iDelete = 0 Then
        $sDelete = "/sDelete /yes"
    ElseIf $iDelete = 1 Then
        $sDelete = "/sDelete " & $iID & " /yes"
    EndIf
    
    $hDel = Run("cmd.exe /c at " & $sName & " " & $sDelete, @SystemDir, @SW_HIDE, 8)
    While ProcessExists("cmd.exe")
        $sRet = StdoutRead($hDel, True)
    WEnd
    If $hDel = 0 Then
        Return SetError(4)
    EndIf
    Return $sRet
EndFunc;==>_TaskDelete

Examples:

#include <Task.au3>

$Return = _TaskGetScheduled()
MsgBox(41,"Scheduled Tasks",$Return)

more examples soon.

Hope you guys like it.

Edit: Edited the UDF, I forgot to add a field for the program/batch file path >.>;

Edit 2: Found another problem. Sorry for all this, I'm 90% asleep and I need coffee :P.

Update: 10/28/08 - Updated to comply with UDF standards.

Edited by dbzfanatic

Share this post


Link to post
Share on other sites



I didn't tested yet, but if it works properly will be very useful. :P


When the words fail... music speaks

Share this post


Link to post
Share on other sites

I haven't been able to test fully either but things work ok here. Please tell me if anything doesn't work and I'll do my best to fix it. :P

Share this post


Link to post
Share on other sites

Hi..

ik like this UDF but isn't at.exe replaced by schtask.exe ? (or will be replaced?)

Best regards,

Emiel


Best regards,Emiel Wieldraaijer

Share this post


Link to post
Share on other sites

I'm not really sure. The at commands run fine here so I didn't even know about schtask.exe, I'll look into it for a future update in case at really will be replaced.

Share this post


Link to post
Share on other sites

type schtasks.exe /? at a cmd prompt to see the syntax.

It's very useful. Once upon a time, I started writing my own little gui for it but didn't get that far with with. It only lists scheduled tasks. I never finished it.

Share this post


Link to post
Share on other sites

I would be the whiner lol

Thanks :P


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

I'll see what I can do, I was just informed by ken82m in another thread that the task scheduler has a COM element so I may look into that instead of using commands. This is actually what I probably will do but for the time being commands are what I'll use. Thank you everyone for your feedback and please feel free to leave any more. :(:P

Share this post


Link to post
Share on other sites

I'll see what I can do, I was just informed by ken82m in another thread that the task scheduler has a COM element so I may look into that instead of using commands. This is actually what I probably will do but for the time being commands are what I'll use. Thank you everyone for your feedback and please feel free to leave any more. :(:P

That would be even better

Thanks

Emiel


Best regards,Emiel Wieldraaijer

Share this post


Link to post
Share on other sites

Well I'm trying to find info on the COM but the MSDN isn't really giving me much help right now. For some of the actions I know you need schtask.dll and/or Vista but that's all I've managed to find out. I haven't really been able to find anything about what I need for ObjCreate() or ObjGet(). If anyone has a link I'd appreciate it >.>;

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Well I'm trying to find info on the COM but the MSDN isn't really giving me much help right now. For some of the actions I know you need schtask.dll and/or Vista but that's all I've managed to find out. I haven't really been able to find anything about what I need for ObjCreate() or ObjGet(). If anyone has a link I'd appreciate it >.>;

Click Here Edited by spudw2k

Share this post


Link to post
Share on other sites

I found that already, what I really dont know (I'm entirely new to COM) is what to use for the call, like InternetExplorer.Application.1, is it a shell call? would it be like Shell.Task.action?

Share this post


Link to post
Share on other sites

I found that already, what I really dont know (I'm entirely new to COM) is what to use for the call, like InternetExplorer.Application.1, is it a shell call? would it be like Shell.Task.action?

O, sorry. I think it'd be either ObjCreate or ObjGet. I don't know squa about c++ so converting code wont be an easy task for me. :P

Share this post


Link to post
Share on other sites

I know I need one of those I just don't know what exactly to use. I know once the object is gotten or created I can do $oTask.Action (object.desired_action) to manipulate the object, I just don't know the object I need. Like I said I don't know if it's a shell object or...anything. I'm utterly lost but if I could just find something that would tell me the object I need I could go from there. I looked in the OLE/COM object viewer but didn't really see anything about task scheduler.

Share this post


Link to post
Share on other sites

Ok this looks helpful thank you :P. From what I can see I need to use

GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

I updated the first post with a more UDF compliant version as well. Thank you all for your feedback and help, keep it coming. :(

Share this post


Link to post
Share on other sites

Ok, from what I can see in order to use the scripting COM API for Task Scheduler the client has to be vista. I want this to be more universal so I think I'll leave it with commands for now. I might update it a bit more with schtask.exe or just make it a bit more streamlined but I don't know when that's going to be. Hope you all like it as-is for now :P.

Share this post


Link to post
Share on other sites

I've looked at that but when you look at the references then actions it says "Minimum Supported Client: Windows Vista" so that's why I said that.

Share this post


Link to post
Share on other sites

I've looked at that but when you look at the references then actions it says "Minimum Supported Client: Windows Vista" so that's why I said that.

Strange

http://msdn.microsoft.com/en-us/library/aa383614(VS.85).aspx

"Task Scheduler 1.0: Client requires Windows Vista, Windows XP, Windows 2000 Professional, Windows Me, or Windows 98. Server requires Windows Server 2008, Windows Server 2003 or Windows 2000 Server. "


Best regards,Emiel Wieldraaijer

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