残念ながら、Windows XPに現在のユーザーとしてタスクをインストールする必要があるため、これらのスクリプトはいずれも自分の問題を解決しませんでした。私はこれらのVBScriptを作成してテストし、それを実行しようとしましたが、タスクを追加したり、その他の方法で機能したりします。
この関数は、システムアカウントで実行されるタスクスケジューラにタスクを追加します。これはWindowsXPでのみテストしましたが、他のバージョンの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
この機能はWindows7では機能しますが、WindowsXPでは機能しません。他の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を気にしない場合は、次のシェルコマンドで実行できます。
schtasks /create /tn "TaskName" /tr "Executable.exe" /sc HOURLY /f
/f
Windows XPでは無効ですので、使用しないでください。残念ながら、Windows XPでは、これにより現在のユーザーのパスワードの入力を求められます。
schtasks /create /tn "TaskName" /tr "Executable.exe" /sc HOURLY
このコマンドに関するいくつかのドキュメントがこことここにあり、他にもたくさんあります。