不幸的是,这些脚本都没有解决我自己的问题,因为我需要以 Windows XP 中的当前用户身份安装任务。我编写并测试了这些 VBScript 以尝试做到这一点,但它们确实添加了任务并且可以正常工作。
该函数将任务添加到在系统帐户下运行的任务调度程序中。我仅在 Windows XP 中对此进行了测试,但我猜它在其他版本的 Windows 中仍然有效。您可能需要对其进行一些调整以使其适合您的需求。基于此。
Function ScheduleTaskWinXP(taskName)
Dim strComputer
strComputer = "."
Dim objWMIService
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
' Win32_ScheduledJob class
' http://msdn.microsoft.com/en-us/library/windows/desktop/aa394399(v=vs.85).aspx
Dim objNewJob
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
' Create method of the Win32_ScheduledJob class
' http://msdn.microsoft.com/en-us/library/windows/desktop/aa389389(v=vs.85).aspx
Dim errJobCreated
errJobCreated = objNewJob.Create("Notepad.exe", _
"********123000.000000-420", True , _
1 OR 4 OR 16, , , taskName)
Out errJobCreated
End Function
此功能在 Windows 7 中有效,但在 Windows XP 中无效。我没有测试其他 Windows 版本。基于此。
Function ScheduleTaskWin7(taskName)
' Task Scheduler Scripting Objects
' http://msdn.microsoft.com/en-us/library/windows/desktop/aa383607(v=vs.85).aspx
'------------------------------------------------------------------
' This sample schedules a task to start on a weekly basis.
'------------------------------------------------------------------
' A constant that specifies a weekly trigger.
const TriggerTypeWeekly = 3
' A constant that specifies an executable action.
const ActionTypeExec = 0
'********************************************************
' Create the TaskService object.
Dim service
Set service = CreateObject("Schedule.Service")
call service.Connect()
'********************************************************
' Get a folder to create a task definition in.
Dim rootFolder
Set rootFolder = service.GetFolder("\")
' The taskDefinition variable is the TaskDefinition object.
Dim taskDefinition
' The flags parameter is 0 because it is not supported.
Set taskDefinition = service.NewTask(0)
'********************************************************
' Define information about the task.
' RegistrationInfo object
' http://msdn.microsoft.com/en-us/library/windows/desktop/aa382100(v=vs.85).aspx
' Set the registration info for the task by
' creating the RegistrationInfo object.
Dim regInfo
Set regInfo = taskDefinition.RegistrationInfo
regInfo.Description = "Start Notepad weekly."
regInfo.Author = "Administrator"
' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.Enabled = True
settings.StartWhenAvailable = True
settings.Hidden = False
'********************************************************
' Create a weekly trigger. Note that the start boundary
' specifies the time of day that the task starts, the
' day-of-week specfies on what day of the week the task
' runs, and the interval specifies what weeks the task runs.
Dim triggers
Set triggers = taskDefinition.Triggers
Dim trigger
Set trigger = triggers.Create(TriggerTypeWeekly)
' Trigger variables that define when the trigger is active
' and the time of day that the task is run. The format of
' this tims is YYYY-MM-DDTHH:MM:SS
Dim startTime, endTime
Dim time
startTime = "2006-05-02T08:00:00" 'Task runs at 8:00 AM
endTime = "2015-05-02T08:00:00"
Out "startTime :" & startTime
Out "endTime :" & endTime
trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.DaysOfWeek = 1
trigger.WeeksInterval = 1 'Task runs every week.
trigger.Id = "WeeklyTriggerId"
trigger.Enabled = True
'***********************************************************
' Create the action for the task to execute.
' Add an action to the task to run notepad.exe.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeExec )
Action.Path = "C:\Windows\System32\notepad.exe"
Out "Task definition created. About to submit the task..."
'***********************************************************
' Register (create) the task.
call rootFolder.RegisterTaskDefinition(taskName, taskDefinition, 6, , , 3)
Out "Task submitted."
End Function
如果你想在当前用户下创建一个任务并且不关心 Windows XP,这个 shell 命令会做:
schtasks /create /tn "TaskName" /tr "Executable.exe" /sc HOURLY /f
/f
在 Windows XP 中无效,所以不要使用它。不幸的是,在 Windows XP 中,这将提示输入当前用户的密码。
schtasks /create /tn "TaskName" /tr "Executable.exe" /sc HOURLY
此处和此处以及许多其他地方都有此命令的一些文档。