다음을 통해 공유


시간 트리거 예제(스크립팅)

이 스크립팅 예제에서는 특정 시간에 메모장을 실행하는 작업을 만드는 방법을 보여줍니다. 작업에는 작업을 활성화하기 위한 시작 경계, 메모장을 실행하는 실행 파일 작업 및 작업을 비활성화하는 끝 경계를 지정하는 시간 기반 트리거가 포함되어 있습니다.

다음 절차에서는 특정 시간에 실행 파일을 시작하도록 작업을 예약하는 방법을 설명합니다.

메모장을 특정 시간에 시작하도록 예약하려면

  1. TaskService 개체를 만듭니다. 이 개체를 사용하면 지정된 폴더에 작업을 만들 수 있습니다.
  2. 작업 폴더를 가져와 작업을 만듭니다. TaskService.GetFolder 메서드를 사용하여 태스크가 저장된 폴더와 TaskService.NewTask 메서드를 가져와 작업을 나타내는 TaskDefinition 개체를 만듭니다.
  3. TaskDefinition 개체를 사용하여 작업에 대한 정보를 정의합니다. TaskDefinition.Settings 속성을 사용하여 작업 스케줄러 서비스가 작업을 수행하는 방법을 결정하는 설정을 정의하고 TaskDefinition.RegistrationInfo 속성을 사용하여 작업을 설명하는 정보를 정의합니다.
  4. TaskDefinition.Triggers 속성을 사용하여 시간 기반 트리거를 만듭니다. 이 속성은 TriggerCollection 개체에 대한 액세스를 제공합니다. TriggerCollection.Create 메서드(만들려는 트리거 유형 지정)를 사용하여 시간 기반 트리거를 만듭니다. 트리거를 만들 때 트리거의 시작 경계 및 끝 경계를 설정하여 트리거를 활성화하고 비활성화합니다. 시작 경계는 작업의 작업을 수행할 시기를 지정합니다.
  5. TaskDefinition.Actions 속성을 사용하여 실행할 태스크에 대한 작업을 만듭니다. 이 속성은 ActionCollection 개체에 대한 액세스를 제공합니다. ActionCollection.Create 메서드를 사용하여 만들려는 작업 유형을 지정합니다. 이 예제에서는 명령줄 작업을 실행하는 작업을 나타내는 ExecAction 개체를 사용합니다.
  6. TaskFolder.RegisterTaskDefinition 메서드를 사용하여 작업을 등록합니다. 이 예제의 경우 작업은 현재 시간에 메모장을 시작하고 30초를 더합니다.

다음 VBScript 예제에서는 작업이 등록된 후 30초 후에 메모장을 실행하도록 작업을 예약하는 방법을 보여 주세요.

'------------------------------------------------------------------
' This sample schedules a task to start notepad.exe 30 seconds
' from the time the task is registered.
'------------------------------------------------------------------

' A constant that specifies a time-based trigger.
const TriggerTypeTime = 1
' A constant that specifies an executable action.
const ActionTypeExec = 0   


'********************************************************
' Create the TaskService object.
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.

' Set the registration info for the task by 
' creating the RegistrationInfo object.
Dim regInfo
Set regInfo = taskDefinition.RegistrationInfo
regInfo.Description = "Start notepad at a certain time"
regInfo.Author = "Author Name"

'********************************************************
' Set the principal for the task
Dim principal
Set principal = taskDefinition.Principal

' Set the logon type to interactive logon
principal.LogonType = 3


' 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 time-based trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeTime)

' Trigger variables that define when the trigger is active.
Dim startTime, endTime

Dim time
time = DateAdd("s", 30, Now)  'start time = 30 seconds from now
startTime = XmlTime(time)

time = DateAdd("n", 5, Now) 'end time = 5 minutes from now
endTime = XmlTime(time)

WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT5M"    'Five minutes
trigger.Id = "TimeTriggerId"
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"

WScript.Echo "Task definition created. About to submit the task..."

'***********************************************************
' Register (create) the task.

call rootFolder.RegisterTaskDefinition( _
    "Test TimeTrigger", taskDefinition, 6, , , 3)

WScript.Echo "Task submitted."



'------------------------------------------------------------------
' Used to get the time for the trigger 
' startBoundary and endBoundary.
' Return the time in the correct format: 
' YYYY-MM-DDTHH:MM:SS. 
'------------------------------------------------------------------
Function XmlTime(t)
    Dim cSecond, cMinute, CHour, cDay, cMonth, cYear
    Dim tTime, tDate

    cSecond = "0" & Second(t)
    cMinute = "0" & Minute(t)
    cHour = "0" & Hour(t)
    cDay = "0" & Day(t)
    cMonth = "0" & Month(t)
    cYear = Year(t)

    tTime = Right(cHour, 2) & ":" & Right(cMinute, 2) & _
        ":" & Right(cSecond, 2)
    tDate = cYear & "-" & Right(cMonth, 2) & "-" & Right(cDay, 2)
    XmlTime = tDate & "T" & tTime 
End Function

작업 스케줄러 사용