Do you have a scripting-related question or problem? You can send your question or problem to [email protected]
I'm trying to use the Windows Management Instrumentation (WMI) Win32_ScheduledJob class to manage scheduled tasks. Unfortunately, I can't even enumerate the scheduled tasks with this class. For example, if I use the Scheduled Task Wizard to schedule a new task, I see the task in the Scheduled Tasks folder, but my script fails to list the task. What am I doing wrong?
The behavior you're seeing is by design. What do I mean by that? The provider that services the Win32_ScheduledJob class—the WMI Win32 Provider (cimwin32.dll)—uses the earlier NetScheduleJob* Win32 APIs instead of the later Task Scheduler APIs (i.e., ITaskScheduler). As a result, the Win32_ScheduledJob class works only with jobs scheduled with the legacy AT command. You can't use the Win32_ScheduledJob class to manage jobs that you scheduled with the Scheduled Task Wizard. Fortunately, you can work around this problem. Excluding third-party solutions, you can have three options:
- Use the schtasks.exe command-line tool.
- Use Microsoft's downloadable Task Scheduler agent.
- Use Windows shell scripting commands.
If you're running Windows Server 2003 or Windows XP, you can use the new schtasks.exe tool, which you can find in the %systemroot%\system32 directory. Schtasks.exe lets you create, change, run, end, delete, and query scheduled tasks from the command line or a script. For example, instead of using your script to list scheduled tasks, you can use the command
C:\>schtasks /query /fo list /v
This command works regardless of the tool you used to schedule the task. In addition, schtasks.exe supports remote computers, alternate credentials, and all the advanced configuration options found in the Scheduled Tasks Properties dialog box. To learn more about schtasks.exe, see the command's usage instructions, which you can find under the "SchTasks" topic in the Help and Support Center under the Start menu in Windows 2003 and XP or by running the command
If you're running Windows 2000, you can use the Task Scheduler agent, which you can download from the Microsoft Download Center (http://www.microsoft.com/downloads/release.asp?releaseid=32465). However, the Task Scheduler agent lets you only create scheduled tasks. In addition, the agent supports only the following configuration options: the Scheduled Task's name, the program to run, program parameters (if any), start time and date, username, and password. In the end, I found the Task Scheduler agent to be of little use, especially when compared with schtasks.exe. However, if you're interested in learning more about the Task Scheduler agent, check out the Microsoft TechNet article "Task Scheduler Using VBScript"(http://www.microsoft.com/technet/prodtechnol/windows2000serv/downloads/taskschd.asp).
No matter which OS you're using, you can use Windows shell scripting commands (e.g., Dir, Copy, Delete) to list, distribute, and remove scheduled tasks across multiple computers. You can find the scheduled tasks (i.e., .job files) in the %systemroot%\Tasks\ folder. If you take this route, make sure your source jobs run under domain-based accounts. Otherwise, you'll likely encounter credential problems when you copy a job configured on one computer to another computer.