Co
(MM club member)
2004-01-09 02:11 PM
Strange behavior of Task Scheduling tools...

FYI,

I wanted to enumerate scheduled tasks on remote computers.

With some help of Shane and Jens I created a script which i can use to schedule a task on multiple computers:
ScheduleTask - Array problem

In this script I use Scheduletask(). Since JT.exe is required for this UDF I thought JT must do the job but it fails...
Quote:


C:\>jt /sm comp1 /se
[TRACE] Setting target computer to 'comp1'
[TRACE] Enumerating jobs and queues
[FAIL ] IEnumJobs::Next hr=0x80070003




..so I created this script and have used it on a WinXP PC:
Code:

Break on
If $strComputer = ""
$strComputer = "."
EndIf


$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2")
If $objWMIService
$colScheduledJobs = $objWMIService.ExecQuery("Select * from Win32_ScheduledJob") ; ,,48)

For Each $objItem In $colScheduledJobs
$name=$objItem.name
$command=$objItem.Command
$days=$objItem.DaysOfWeek
$mstime=$objItem.StartTime
$ro=RedirectOutput("d:\log\"+@WKSTA+".log",0)
? 'task: '+$name+Chr(9)+$command+Chr(9)+$day+Chr(9)+$mstime+Chr(13)+Chr(10)
If $strComputer = "."
$strComputer = @wksta
EndIf

Next

;sleep 10
EndIf



It works but it only shows tasks scheduled by AT and it doesn't show tasks that are scheduled by JT.exe, Schtasks or the Scheduled Tasks GUI. It must be a WMI / Win32_ScheduledJob problem because the following VBscript gives the same output:

Code:
 strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colScheduledJobs = objWMIService.ExecQuery _
("Select * from Win32_ScheduledJob")
For Each objJob in colScheduledJobs
Wscript.Echo "Caption: " & objJob.Caption
Wscript.Echo "Command: " & objJob.Command
Wscript.Echo "Days Of Month: " & objJob.DaysOfMonth
Wscript.Echo "Days Of Week: " & objJob.DaysOfWeek
Wscript.Echo "Description: " & objJob.Description
Wscript.Echo "Elapsed Time: " & objJob.ElapsedTime
Wscript.Echo "Install Date: " & objJob.InstallDate
Wscript.Echo "Interact with Desktop: " & objJob.InteractWithDesktop
Wscript.Echo "Job ID: " & objJob.JobID
Wscript.Echo "Job Status: " & objJob.JobStatus
Wscript.Echo "Name: " & objJob.Name
Wscript.Echo "Notify: " & objJob.Notify
Wscript.Echo "Owner: " & objJob.Owner
Wscript.Echo "Priority: " & objJob.Priority
Wscript.Echo "Run Repeatedly: " & objJob.RunRepeatedly
Wscript.Echo "Start Time: " & objJob.StartTime
Wscript.Echo "Status: " & objJob.Status
Wscript.Echo "Time Submitted: " & objJob.TimeSubmitted
Wscript.Echo "Until Time: " & objJob.UntilTime
Next


Quote:


Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Caption:
Command: Notepad.exe
Days Of Month:
Days Of Week: 21
Description:
Elapsed Time:
Install Date:
Interact with Desktop: True
Job ID: 1
Job Status: Success
Name:
Notify:
Owner:
Priority:
Run Repeatedly: True
Start Time: ********213000.000000+060
Status:
Time Submitted:
Until Time:
Caption:
Command: notepad
Days Of Month:
Days Of Week:
Description:
Elapsed Time:
Install Date:
Interact with Desktop: False
Job ID: 3
Job Status: Success
Name:
Notify:
Owner:
Priority:
Run Repeatedly: False
Start Time: ********170000.000000+060
Status:
Time Submitted:
Until Time:
Exit code: 0 , 0000h






The only tool wich gives all tasks and works on remote pc's is the XP schtasks command. unfortunately you cann't use it with previous versions of Windows

D:\>schtasks /query /S comp2

TaskName Next Run Time Status
==================================== ======================== ===============
test 17:00:00, 9-1-2003


Maybe it is an easy problem and did I used the wrong JT parameters..


AllenAdministrator
(KiX Supporter)
2004-01-09 04:13 PM
Re: Strange behavior of Task Scheduling tools...

I have a version of schtasks.exe that works with Win2k. If you are interested, reply with your email address.

Sealeopard
(KiX Master)
2004-01-09 04:17 PM
Re: Strange behavior of Task Scheduling tools...

Which is a byte-hacked version of the Windows XP version as detailed in http://www.jsiinc.com/SUBK/tip5300/rh5335.htm

However, the advantage of JT.EXE is that it works under any Windows that has the Task Scheduler installed and we already have a complete set of UDFs to manage scheduled tasks either via the ScheduleTask() UDF for simple schedules or the tsControl() UDF collection for advanced task scheduling.


Sealeopard
(KiX Master)
2004-01-09 04:20 PM
Re: Strange behavior of Task Scheduling tools...

Quote:

It works but it only shows tasks scheduled by AT and it doesn't show tasks that are scheduled by JT.exe,


This is by design and well documented on MSDN. The WMI object can only interact with AT-type tasks but not task scheduled otherwise, e.g. via Task Scheudler or JT.EXE.


AllenAdministrator
(KiX Supporter)
2004-01-09 04:29 PM
Re: Strange behavior of Task Scheduling tools...

Jens... if you have installed XP SP1, the version of schtasks.exe is slightly different and the link you provide no longer works.

Co, offer still stands if you need it.