-
Notifications
You must be signed in to change notification settings - Fork 13
/
PS_ScheduleTask.ps1
53 lines (41 loc) · 1.63 KB
/
PS_ScheduleTask.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# https://stackoverflow.com/a/35777432
# https://stackoverflow.com/a/29337370
$Target = "10.10.11.113"
$Domain = "avatar.local"
$User = "iroh"
$Password = "3JyE63D%xu!4mBwnTHtvY8bhU2Z2r^M"
$Command = "powershell.exe"
$Arguments = "-e aaa"
$TaskName = "ps name"
$TaskDesc = "ps desc"
$ExecuteTime = (Get-Date).AddDays(1)
$ExpireTime = $ExecuteTime.AddMinutes(2)
$ShedService = new-object -comobject "Schedule.Service"
$ShedService.Connect($Target, $User, $Domain, $Password)
$Task = $ShedService.NewTask(0)
$Task.RegistrationInfo.Description = "$TaskDesc"
$Task.Settings.Enabled = $true
$Task.Settings.AllowDemandStart = $true
$Task.Settings.DeleteExpiredTaskAfter = "PT0S"
$Task.Settings.ExecutionTimeLimit = "PT1H"
$trigger = $task.triggers.Create(1) # Creates a "One time" trigger
# TASK_TRIGGER_EVENT 0
# TASK_TRIGGER_TIME 1
# TASK_TRIGGER_DAILY 2
# TASK_TRIGGER_WEEKLY 3
# TASK_TRIGGER_MONTHLY 4
# TASK_TRIGGER_MONTHLYDOW 5
# TASK_TRIGGER_IDLE 6
# TASK_TRIGGER_REGISTRATION 7
# TASK_TRIGGER_BOOT 8
# TASK_TRIGGER_LOGON 9
# TASK_TRIGGER_SESSION_STATE_CHANGE 11
$trigger.StartBoundary = $ExecuteTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
$trigger.EndBoundary = $ExpireTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
$trigger.Enabled = $true
$Action = $Task.Actions.Create(0)
$action.Path = $Command
$action.Arguments = $Arguments
Try {$taskFolder = $ShedService.GetFolder("\$taskpath")}
catch {$taskFolder = $ShedService.GetFolder("\").CreateFolder("$taskpath")}
$result = $taskFolder.RegisterTaskDefinition("$TaskName",$Task,6,"System",$null,5)