-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.ps1
More file actions
78 lines (64 loc) · 3.06 KB
/
Copy pathScript.ps1
File metadata and controls
78 lines (64 loc) · 3.06 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<#
.SYNOPSIS
This script will create a scheduled task or an
automatic task.
.DESCRIPTION
Scheduled: Calls another script every hour.
Automatic: Calls another script every time a
different task finishes.
#>
#Stores Script's Current Location
$sCurrDir = $myinvocation.mycommand.path
$sCurrDir = $sCurrDir.Replace("\" + $myinvocation.MyCommand, "")
#Controls Script Actions
function Start-Main{
Create-AutoTask
}
#Creates Scheduled Task
function Create-ScheduledTask{
#Creates Scheduled Task Actions
$TaskAction1 = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument "-ExecutionPolicy Bypass -noprofile -command ""&{ start-process powershell -ArgumentList '-noprofile -file $sCurrDir\--ScriptToRun--.ps1' -verb RunAs}"""
#Assigns Username
$TaskUsername = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
#Creates Scheduled Task Settings
$TaskSettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
#Creates Triggers
$TaskTrigger = New-ScheduledTaskTrigger -AtLogOn
#Create Scheduled Task
$Task = New-ScheduledTask -Action $TaskAction1 -Principal $TaskUsername -Trigger $TaskTrigger -Settings $TaskSettings
#Register Scheduled Task
Register-ScheduledTask "--TaskName--" -InputObject $Task -Force
#Configure Scheduled Task
$Task = Get-ScheduledTask -TaskName "--TaskName--"
#Omit Duration for infinite duration
#$Task.Triggers.Repetition.Duration = "P1D"
#Interval to repeat task (60M = 1 hour)
$Task.Triggers.Repetition.Interval = "PT1M"
#Update Scheduled Task
$Task | Set-ScheduledTask -User "NT AUTHORITY\SYSTEM"
}
#Creates Automatic Task
function Create-AutoTask{
$taskName = "UACLevelAuto"
$Path = 'PowerShell.exe'
$Arguments = "-ExecutionPolicy Bypass -noprofile -command ""&{ start-process powershell -ArgumentList '-noprofile -file $sCurrDir\--ScriptToRun--.ps1' -verb RunAs}"""
$Service = new-object -ComObject ("Schedule.Service")
$Service.Connect()
$RootFolder = $Service.GetFolder("\")
$TaskDefinition = $Service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = ''
$TaskDefinition.Settings.Enabled = $True
$TaskDefinition.Settings.AllowDemandStart = $True
$TaskDefinition.Settings.DisallowStartIfOnBatteries = $False
$Triggers = $TaskDefinition.Triggers
$Trigger = $Triggers.Create(0)
$Trigger.Enabled = $true
$Trigger.Id = '102'
$Trigger.Subscription = "<QueryList><Query Id=""0"" Path=""Microsoft-Windows-TaskScheduler/Operational""><Select Path=""Microsoft-Windows-TaskScheduler/Operational"">*[EventData[@Name='TaskSuccessEvent'][Data[@Name='TaskName']='--TaskNameHere--']]</Select></Query></QueryList>"
$Action = $TaskDefinition.Actions.Create(0)
$Action.Path = $Path
$action.Arguments = $Arguments
$RootFolder.RegisterTaskDefinition($taskName, $TaskDefinition, 6, "System", $null, 5) | Out-Null
}
#Starts Script
Start-Main