Google
 
Web itpro-blogger.blogspot.com
金曜日, 7月 15, 2005

[VB.NET]タスクスケジュール

[参照設定」->「参照の追加」->「COM」->「Microsoft WMI Scripting V1.1 Library」を追加する。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strErrMsg As String
Dim intErrNo As Integer
Dim strErrDetail As String
Dim strErrType As String
Dim Job As WbemScripting.SWbemObject
Dim Locator As WbemScripting.SWbemLocator
Dim Service As Object

Try

Dim Ret As Int32
Dim ID As Int32
Dim MesStr As String

Locator = New WbemScripting.SWbemLocator
'Set Service = Locator.ConnectServer("ComputarName","root\cimv2","UserName","Password")
Service = Locator.ConnectServer(".")
Job = Service.Get("Win32_ScheduledJob")

'Command    :Name of the executable program, batch file, or script to be run.
'StartTime   :UTC time to run the job. This is of the form YYYYMMDDHHMMSS.MMMMMM(+-)OOO,
' where YYYYMMDD must be replaced by ********. Example: ********123000.000000-420,
' which implies 12:30 P.M. Pacific time with daylight saving time in effect.
'RunRepeatedly :Indicates whether the scheduled job should run repeatedly on the days that
' the job is scheduled. The default is FALSE. This parameter is optional.
'DaysOfWeek   :Days of the week when the task should be run. Values are:
' 1 - 月曜日、2 - 火曜日、4 - 水曜日、8 - 木曜日、16 - 金曜日、32 - 土曜日、64 - 日曜日
' To run the task on multiple days, use a logical OR to combine values.
' For example, to run a task on Tuesday, Thursday, and Saturday, use the following code:
' 2 OR 8 OR 32
' To run a task on multiple days, you must set the RunRepeatedly parameter to True.
'DaysOfMonth  :Days of the month when the job is scheduled to run;
' used only when the RunRepeatedly parameter is True. This parameter is optional.
'InteractWithDesktop:Indicates whether the specified job should be interactive
' (meaning that a user can give input to the scheduled job while it is executing).
' The default is False. This parameter is optional and is rarely set to True. In general,
' the reason to use a scheduled task is to allow a task to be carried out without any user interaction.
'JobID     :Identifier number of the job. This parameter is a handle to the job being scheduled on this computer.

'Ret = Job.Create("Command", "StartTime[YYYYMMDDHHMMSS.MMMMMM(+-)OOO]", RunRepeatedly, DaysOfWeek, DaysOfMonth, InteractWithDesktop, JobID)
Ret = CLng(Job.Create("notepad.exe", "********123000.000000-900", False, 1, 1, False, ID))

If Ret = 0 Then
MsgBox("タスクを ID:" & ID & " で追加しました。")
Else
MsgBox("タスク追加に失敗しました。")
End If


Catch ex As Exception

strErrMsg = Err.Description
intErrNo = Err.Number
strErrDetail = ex.ToString()
strErrType = ex.GetType().ToString()

MsgBox(strErrMsg & vbCrLf & intErrNo & vbCrLf & strErrDetail & vbCrLf & strErrType)

Finally

Job = Nothing
Service = Nothing
Locator = Nothing

End Try

End Sub

■参考サイト
【- タスクスケジュールを追加する [Win32_ScheduledJob] -】
http://wmifun.atinfinity.net/sample/win32_scheduledjobb.html
【VBS for WMI】
http://www.geocities.jp/maru3128/wmi.html
Microsoft Windows 2000 Scripting Guide - Creating Scheduled Tasks
http://www.microsoft.com/technet/scriptcenter/guide/sas_man_rsxs.mspx
Windows 2000 タスク スケジューラ
http://www.microsoft.com/japan/
technet/prodtechnol/windows2000serv/maintain/featusability/tsksched.mspx

Hey, Scripting Guy!
http://www.microsoft.com/japan/
technet/scriptcenter/resources/qanda/sept04/hey0922.mspx








<< Home

This page is powered by Blogger. Isn't yours?