-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.ps1
57 lines (45 loc) · 1.46 KB
/
task.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
54
55
56
function Register-LogonTask {
[CmdletBinding()]
Param(
[string] $Name,
[string] $Path = "\",
[string] $Executable,
[string] $Argument,
[switch] $Force = $false
)
$action = New-ScheduledTaskAction -Execute $Executable -Argument $Argument
$trigger = New-ScheduledTaskTrigger -AtLogOn -User "${env:\USERDOMAIN}\${env:USERNAME}"
$settings = New-ScheduledTaskSettingsSet `
-StartWhenAvailable `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-ExecutionTimeLimit (New-TimeSpan)
$task = New-ScheduledTask `
-Trigger $trigger `
-Action $action `
-Settings $settings
Register-ScheduledTask `
-TaskName $Name `
-TaskPath $Path `
-InputObject $task `
-Force:$Force `
| Out-Null
<#
.SYNOPSIS
Registers a task to run at user logon.
.DESCRIPTION
Registers a scheduled task that runs when the user logs in.
.PARAMETER Name
Name of the task.
.PARAMETER Path
Path to the task. Default: root path ("\").
.PARAMETER Executable
The path to the executable to run.
.PARAMETER Argument
The argument to pass to the executable.
.PARAMETER Force
If specified, overwrites the task if it already exists.
.EXAMPLE
Register-LogonTask -Name "MyTask" -Path "\MyFolder" -Executable "C:\MyFolder\MyExecutable.exe" -Argument "MyArgument"
#>
}