Jump to content

Create a scheduled task ?


Recommended Posts

Hi,

I'm a very new user of AutoIt and i would like to know if i can use it to create a task in the windows tasks scheduler.

Of course i would also like to know what functions i'm supposed to use for that.

Thank you all

:)

Link to comment
Share on other sites

Hi,

I'm a very new user of AutoIt and i would like to know if i can use it to create a task in the windows tasks scheduler.

Of course i would also like to know what functions i'm supposed to use for that.

Thank you all

:)

Hi,

You can try to use the AT windows command:

Run("AT.exe", @SystemDir, "flags here")

Or

RunWait("AT.exe", @SystemDir, "flags here")

Cheers

Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Link to comment
Share on other sites

  • 9 months later...

Make sure you are running as an administrator and not just a user.

I do believe that schtasks was implemted into the Windows system in some versions of Windows 2000 and above (mostly just the server packages like Windows 2003 Server Edition), I do know that Microsoft is trying to phase out the AT.exe command. If you can't seem to be able to use schtasks.exe then use the AT.exe command. Also, I would do a search for schtasks.exe in the Microsoft KB and see what you can find out there. I know there is alot of help when you search for it in Google. Alot of people as this question constantly.

Spoiler

 

"...Yes, I am a criminal. My crime is that of curiosity. My crime is that of judging people by what they say and think, not what they look like.My crime is that of outsmarting you, something that you will never forgive me for." --The Mentor

"Mess with the best, die like the rest!!" -Hackers

"This above all to thine own self be true." -William Shakespeare

"Corruption is only as corrupted as the individual makes it out to be." -i386

 

style7,AutoIt.png

 

 

Link to comment
Share on other sites

Hi,

@i386: I am running as an admin. Unfortunately, I was unable to find schtasks.exe on any path in my system (did a file search).

You mentioned, I should try AT.exe. I'm not sure wether it has the functionality I want or not. It should start the computer up from hibernating. I didnt find any information about parameters of this command where I would be able to set "startup from hibernating" or like that.

I know, that windows scheduler stores the startup-time in the compuer's RTC, and that the later startup procedure is running on BIOS level first.

It would be perfect, if I had an AutoIt function that does the same thing without using the windows scheduler.

Zoli

Link to comment
Share on other sites

I found this VBScript on the forums:

' This code schedules a task to run every Sunday at 1:30AM.

' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' Publisher: O'Reilly Media
' ISBN: 0-596-00633-0
' Book web site: http://rallenhome.com/books/winsckbk/code.html
' ---------------------------------------------------------------

const MON = 1
const TUE = 2
const WED = 4
const THU = 8
const FRI = 16
const SAT = 32
const SUN = 64

' ------ SCRIPT CONFIGURATION ------
strComputer  = "."
strCommand   = "c:\perl\bin\perl.exe c:\scripts\diskchecker.pl"
strStartTime = "********013000.000000-240"  ' 01:30 EDT 
               'YYYYMMDDHHMMSS.MMMMMM-/+TZO 
boolRepeat   = TRUE  ' Repeat the task periodically
intWeekDay   = SUN   ' Repeat task every Sunday
intMonthDay  = ""   ' Set this if you want the task to repeat monthly
boolInteract = FALSE ' Do not interact with the desktop
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "")
set objNewTask = objWMI.Get("Win32_ScheduledJob")
intRC = objNewTask.Create(strCommand,   _
                          strstartTime, _
                          boolRepeat,   _
                          intWeekDay,   _
                          intMonthDay,  _
                          boolInteract, _
                          intJobID) 
if intRC <> 0 then
   Wscript.Echo "Error creating task: " & intRC
else
   WScript.Echo "Successfully scheduled task."
   WScript.Echo "JobID: " & intJobID
end if

Das Häschen benutzt Radar

Link to comment
Share on other sites

Cool, nice script. Do you know, if there is an option for automatic startup I can instert in it? I mean, Windows has the option "resume computer from standby" or something like that in it's Schedule creation dialog/wizard, so I think there must be an equivalent thing as a command or an option to that object call the script uses. That's exactly what I need.

Zoli

Link to comment
Share on other sites

worked this out yesterday when I saw the VB script

Opt("MustDeclareVars", 1)
Opt("TrayIconHide", 1)

;Win32_ScheduledJob Class
;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/Win32_ScheduledJob.asp

;Create Method of the Win32_ScheduledJob Class
;http://msdn2.microsoft.com/en-us/library/aa389389(VS.85).aspx


; The Win32_ScheduledJob class represents a job scheduled using the network management schedule service functions
; (also known as "Job" and "AT command" functions).
; Note that this is different from the tasks scheduled using the Windows 2000 Task Scheduler.
; This class is only instrumented on Windows NT 4.0 and later.
; Each job scheduled against the schedule service is stored persistently
; (the scheduler will know to start the job even after a reboot)
; and is executed at the specified time and day of the week and/or month.
; If the computer is not active or if the scheduled service is not running at the specified job time
; the schedule service will run the specified job on the next day at the specified time.
; Scheduled jobs are scheduled with respect to Universal Coordinated Time (UTC), i.e. with bias offset from GMT.
; This means that a job can be specified using any time zone specification.
; The Win32_ScheduledJob class will return the local time with UTC offset when enumerating an object and convert
; to local time when creating new jobs.
; For example a job specified to run on a computer in Boston at 10:30 pm Monday PST time will be scheduled
; to run locally at 1:30am Tuesday EST.
; It should be noted that a client must take into account whether daylight savings time is in operation on the local computer
; and if so subtract a bias of 60 minutes from the UTC offset.

; This WMI code same as AT.exe
; List jobs programmatically with Win32_ScheduledJob, Caveat: wont list jobs scheduled with schtasks.exe
; Scheduled tasks folder in Winxp displays jobs as 'At1', 'At2' etc.
Local $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Local $objWMIService, $objShare, $objInParam, $objOutParams
Local $strComputer = @ComputerName ; "." "localhost"

Local $scheduledprog = @SystemDir & "\notepad.exe"
Local $time = "1321"    ; 24hr Time: 1:20PM
Local $tzo = "300"      ; Timezone: EST (5 * 60)

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$objShare = $objWMIService.Get("Win32_ScheduledJob")
$objInParam = $objShare.Methods_("Create").inParameters.SpawnInstance_()
$objInParam.Properties_.Item("Command") = $scheduledprog 
$objInParam.Properties_.Item("DaysOfMonth") =  "" ; only for scheduling repeat events
$objInParam.Properties_.Item("DaysOfWeek") = ""   ; only for scheduling repeat events
$objInParam.Properties_.Item("InteractWithDesktop") = True ; False = Run process as System
$objInParam.Properties_.Item("RunRepeatedly") =  False     ; False = Run one time only
$objInParam.Properties_.Item("StartTime") =  "********" & $time &"00.000000-" & $tzo
                                            ; YYYYMMDDHHMMSS.MMMMMM-/+TZO - cant use Year/Month/Day or Seconds
$objOutParams = $objWMIService.ExecMethod("Win32_ScheduledJob", "Create", $objInParam)
ConsoleWrite("Out Parameters: " & @CRLF)
ConsoleWrite("JobId: " & $objOutParams.JobId & @CRLF)
ConsoleWrite("ReturnValue: " & $objOutParams.ReturnValue & @CRLF)

Func MyErrFunc()
   Local $HexNumber = Hex($oMyError.number,8)
   ConsoleWrite("Debug:  Intercepted COM Error:  Number = " & $HexNumber & _
        "  Windescription = " & $oMyError.windescription & @LF)
   SetError(1)
EndfuncoÝ÷ ÚX¬¶:°è¬rZ²K
99b²×jëh×6; Generated by AutoIt Scriptomatic

Opt("MustDeclareVars", 1)
Local $strComputer = "localhost"
Local $colItems = "", $Output="", $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$Output &= "Computer: " & $strComputer  & @CRLF
$Output &= "==========================================" & @CRLF
Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ScheduledJob", "WQL", 0x30)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output &= "Caption: " & $objItem.Caption & @CRLF
      $Output &= "Command: " & $objItem.Command & @CRLF
      $Output &= "DaysOfMonth: " & $objItem.DaysOfMonth & @CRLF
      $Output &= "DaysOfWeek: " & $objItem.DaysOfWeek & @CRLF
      $Output &= "Description: " & $objItem.Description & @CRLF
      $Output &= "ElapsedTime: " & WMIDateStringToDate($objItem.ElapsedTime) & @CRLF
      $Output &= "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
      $Output &= "InteractWithDesktop: " & $objItem.InteractWithDesktop & @CRLF
      $Output &= "JobId: " & $objItem.JobId & @CRLF
      $Output &= "JobStatus: " & $objItem.JobStatus & @CRLF
      $Output &= "Name: " & $objItem.Name & @CRLF
      $Output &= "Notify: " & $objItem.Notify & @CRLF
      $Output &= "Owner: " & $objItem.Owner & @CRLF
      $Output &= "Priority: " & $objItem.Priority & @CRLF
      $Output &= "RunRepeatedly: " & $objItem.RunRepeatedly & @CRLF
      $Output &= "StartTime: " & WMIDateStringToDate($objItem.StartTime) & @CRLF
      $Output &= "Status: " & $objItem.Status & @CRLF
      $Output &= "TimeSubmitted: " & WMIDateStringToDate($objItem.TimeSubmitted) & @CRLF
      $Output &= "UntilTime: " & WMIDateStringToDate($objItem.UntilTime) & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_ScheduledJob" )
Endif

Func WMIDateStringToDate($dtmDate)
    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

Func MyErrFunc()
   Local $HexNumber = Hex($oMyError.number,8)
   ConsoleWrite("Debug:  Intercepted COM Error:  Number = " & $HexNumber & _
        "  Windescription = " & $oMyError.windescription & @LF)
   SetError(1)
Endfunc

I see fascists...

Link to comment
Share on other sites

Probably we should do a Google search on this string: Win32_ScheduledJob

and look for example scripts.

This is why I recommend using SCHTASKS.EXE and not that WMI technique: (quoted from MSDN):

Win32_ScheduledJob Class

The Win32_ScheduledJob WMI class represents a job created with the AT command. The Win32_ScheduledJob class does not represent a job created with the Scheduled Task Wizard from the Control Panel. You cannot change a task created by WMI in the Scheduled Tasks UI.

None of these things are problems with SCHTASKS.EXE. It can modify and otherwise act on jobs created in the Scheduled Tasks GUI, and create new job that will be usable through that UI.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

So Googling Schtasks instead:

APPLIES TO

Microsoft Windows Server 2003, Datacenter Edition (32-bit x86)

Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)

Microsoft Windows Server 2003, Standard Edition (32-bit x86)

Microsoft Windows Server 2003, Web Edition

Microsoft Windows XP Professional for Itanium-based systems

Microsoft Windows Small Business Server 2003 Standard Edition

Microsoft Windows Small Business Server 2003 Premium Edition

Schtasks /Create

Creates a new scheduled task. Syntax:

schtasks /create/tn TaskName /tr TaskRun /sc schedule [/mo modifier] [/d day] [/m month[,month...] [/i IdleTime] [/st StartTime] [/sd StartDate] [/ed EndDate] [/du duration] [/s computer [/u [domain\]user /p password]] [/ru {[Domain\]User | "System"} [/rp Password]] /?

It doesn't look like we have the desired functionality, and it doesn't seem to be available on my version of XP. Someone on the forums found a way to wake on LAN - I have the script but I haven't tried it. Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

So Googling Schtasks instead:

It doesn't look like we have the desired functionality, and it doesn't seem to be available on my version of XP. Someone on the forums found a way to wake on LAN - I have the script but I haven't tried it.

Yeah, backward compatibility is an issue. It doesn't come with Win2K baseline, but in one of the service packs or resource kits for that.

Since I have multiple server versions running around here, I just use the Win2K3 copy and it works with everything I have (Win2K SP4 minimum).

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 1 month later...

I have just tried to copy the schtasks.exe (File-Version 5.2.3790.0) from one of my Win 2003 Server Installations to a Server running Win 2000 Server (SP4), but this one does not run. I get a error message " ERROR: The target system must be running Windows XP or above."

So what version of schtasks do you use or do I need to copy more files in order to get the schtasks.exe running on my W2K Server ?

Link to comment
Share on other sites

I have just tried to copy the schtasks.exe (File-Version 5.2.3790.0) from one of my Win 2003 Server Installations to a Server running Win 2000 Server (SP4), but this one does not run. I get a error message " ERROR: The target system must be running Windows XP or above."

So what version of schtasks do you use or do I need to copy more files in order to get the schtasks.exe running on my W2K Server ?

It's included in the Win2K resource kit from M$. I'm pretty sure I just used the copy from one of my Win2K3 SP1 servers, and it worked. Problem is, the Win2K servers have since been rebuilt with Win2K3, so I can't go back and prove it now.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...