-
Notifications
You must be signed in to change notification settings - Fork 1
/
Installer.ps1
335 lines (299 loc) · 11.8 KB
/
Installer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
$cronScriptPath = Join-Path $env:ProgramData -ChildPath 'Scheduled Scripts'
$cronVbsLauncher = Join-Path $cronScriptPath -ChildPath 'Scheduler.vbs'
# ---[ START CONFIG ]---
$cronFolderPath = 'System Scripts'
$taskProfiles = @{
'Daily Tasks' = @{
Description = 'Runs all scripts under "%PROGRAMDATA%\Scheduled Scripts\Tasks\Daily\*.ps1" every day from about 3:00PM to 3:30PM. All scripts will execute with the "NT AUTHORITY\NETWORKSERVICE" privileges. No user needs to be logged on, but the PC should be running on AC power. This task will not execute again until the next scheduled time if it missed its scheduled start for any reason.'
Action = @{
Execute = 'wscript.exe'
Arguments = '"{0}" Daily' -f $cronVbsLauncher
}
StartTime = '15:00:00'
}
'Daily Tasks (battery exempted)' = @{
Description = 'Runs all scripts under "%PROGRAMDATA%\Scheduled Scripts\Tasks\Battery Exempted\Daily\*.ps1" every day from about 3:00PM to 3:30PM. All scripts will execute with the "NT AUTHORITY\NETWORKSERVICE" privileges. No user needs to be logged on. This task will run on both battery and AC power. This task will not execute again until the next scheduled time if it missed its scheduled start for any reason.'
Action = @{
Execute = 'wscript.exe'
Arguments = '"{0}" EnsureDaily' -f $cronVbsLauncher
}
StartTime = '15:00:00'
}
'Weekly Tasks' = @{
Description = 'Runs all scripts under "%PROGRAMDATA%\Scheduled Scripts\Tasks\Weekly\*.ps1" every Monday from about 4:00PM to 4:30PM. All scripts will execute with the "NT AUTHORITY\NETWORKSERVICE" privileges. No user needs to be logged on, but the PC should be running on AC power. This task will not execute again until the next scheduled time if it missed its scheduled start for any reason.'
Action = @{
Execute = 'wscript.exe'
Arguments = '"{0}" Weekly' -f $cronVbsLauncher
}
StartTime = '16:00:00'
}
'Weekly Tasks (battery exempted)' = @{
Description = 'Runs all scripts under "%PROGRAMDATA%\Scheduled Scripts\Tasks\Battery Exempted\Weekly\*.ps1" every Monday from about 4:00PM to 4:30PM. All scripts will execute with the "NT AUTHORITY\NETWORKSERVICE" privileges. No user needs to be logged on. This task will run on both battery and AC power. This task will not execute again until the next scheduled time if it missed its scheduled start for any reason.'
Action = @{
Execute = 'wscript.exe'
Arguments = '"{0}" EnsureWeekly' -f $cronVbsLauncher
}
StartTime = '16:00:00'
}
'Monthly Tasks' = @{
Description = 'Runs all scripts under "%PROGRAMDATA%\Scheduled Scripts\Tasks\Monthly\*.ps1" on the first day of every month from about 5:00PM to 5:30PM. All scripts will execute with the "NT AUTHORITY\NETWORKSERVICE" privileges. No user needs to be logged on, but the PC should be running on AC power. This task will not execute again until the next scheduled time if it missed its scheduled start for any reason.'
Action = @{
Execute = 'wscript.exe'
Arguments = '"{0}" Monthly' -f $cronVbsLauncher
}
StartTime = '17:00:00'
}
'Monthly Tasks (battery exempted)' = @{
Description = 'Runs all scripts under "%PROGRAMDATA%\Scheduled Scripts\Tasks\Battery Exempted\Monthly\*.ps1" on the first day of every month from about 5:00PM to 5:30PM. All scripts will execute with the "NT AUTHORITY\NETWORKSERVICE" privileges. No user needs to be logged on. This task will run on both battery and AC power. This task will not execute again until the next scheduled time if it missed its scheduled start for any reason.'
Action = @{
Execute = 'wscript.exe'
Arguments = '"{0}" EnsureMonthly' -f $cronVbsLauncher
}
StartTime = '17:00:00'
}
}
# ---[ END CONFIG ]---
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if (-not $isAdmin)
{
throw "This installer requires administrative privileges."
}
if (Test-Path $cronScriptPath -PathType Container)
{
throw "Remove existing installation first!"
}
$taskXmlTemplate = @'
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>{{ $creationTimestamp }}</Date>
<Author>NT AUTHORITY\TrustedInstaller</Author>
<URI>\{{ $taskPath }}\{{ $taskName }}</URI>
<Description>{{ $description }}</Description>
</RegistrationInfo>
<Principals>
<Principal id="Author">
<UserId>S-1-5-20</UserId>
</Principal>
</Principals>
<Settings>
{{ $batterySetting }}
<ExecutionTimeLimit>PT30M</ExecutionTimeLimit>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<IdleSettings>
<Duration>PT10M</Duration>
<WaitTimeout>PT1H</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
</Settings>
<Triggers>
<CalendarTrigger>
<StartBoundary>{{ $startTimestamp }}</StartBoundary>
{{ $calendarConfig }}
</CalendarTrigger>
</Triggers>
<Actions Context="Author">
<Exec>
<Command>{{ $execute }}</Command>
<Arguments>{{ $argument }}</Arguments>
</Exec>
</Actions>
</Task>
'@
$batteryConfig = @'
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
'@
$batteryExemptConfig = @'
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
'@
$dailyConfig = @'
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
'@
$weeklyConfig = @'
<ScheduleByWeek>
<WeeksInterval>1</WeeksInterval>
<DaysOfWeek>
<Monday />
</DaysOfWeek>
</ScheduleByWeek>
'@
$monthlyConfig = @'
<ScheduleByMonth>
<Months>
<January />
<February />
<March />
<April />
<May />
<June />
<July />
<August />
<September />
<October />
<November />
<December />
</Months>
<DaysOfMonth>
<Day>1</Day>
</DaysOfMonth>
</ScheduleByMonth>
'@
$comService = New-Object -ComObject Schedule.Service
$comService.Connect()
$rootFolder = $comService.GetFolder('\')
try
{
$baseFolder = $rootFolder.GetFolder($cronFolderPath)
}
catch
{
if ($_.FullyQualifiedErrorId -ne 'System.IO.FileNotFoundException')
{
throw $_
}
$baseFolder = $rootFolder.CreateFolder('\' + $cronFolderPath)
}
$taskProfiles.Keys | ForEach-Object {
$taskName = $_
$registerScheduledTaskParams = $taskProfiles."$taskName"
$xmlContent = $taskXmlTemplate
$taskXmlParams = @{
'taskPath' = $cronFolderPath
'taskName' = $taskName
'description' = $taskProfiles."$taskName".Description
'execute' = $taskProfiles."$taskName".Action.Execute
'argument' = $taskProfiles."$taskName".Action.Arguments
'creationTimestamp' = Get-Date -Format 'yyyy-MM-ddTHH:mm:ss'
'startTimestamp' = Get-Date -Format ('yyyy-MM-ddT{0}' -f $taskProfiles."$taskName".StartTime)
}
$taskXmlParams.Keys | ForEach-Object {
$xmlContent = $xmlContent.Replace('{{ $' + $_ + ' }}', $taskXmlParams."$_")
}
if ($taskName -like '*battery exempted*')
{
$xmlContent = $xmlContent.Replace('{{ $batterySetting }}', $batteryExemptConfig)
}
else
{
$xmlContent = $xmlContent.Replace('{{ $batterySetting }}', $batteryConfig)
}
if ($taskName -like '*Monthly*')
{
$xmlContent = $xmlContent.Replace('{{ $calendarConfig }}', $monthlyConfig)
}
elseif ($taskName -like '*Weekly*')
{
$xmlContent = $xmlContent.Replace('{{ $calendarConfig }}', $weeklyConfig)
}
elseif ($taskName -like '*Daily*')
{
$xmlContent = $xmlContent.Replace('{{ $calendarConfig }}', $dailyConfig)
}
else
{
throw "task name must be *Monthly|Weekly|Daily*"
}
$taskItem = $comService.NewTask(0)
$taskItem.XmlText = $xmlContent
$baseFolder.RegisterTaskDefinition($taskName, $taskItem, 6, $null, $null, 5, $null)
}
$computerStartXml = @'
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>{{ $creationTimestamp }}</Date>
<Author>NT AUTHORITY\TrustedInstaller</Author>
<URI>\{{ $taskPath }}\{{ $taskName }}</URI>
<Description>{{ $description }}</Description>
</RegistrationInfo>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
</Principal>
</Principals>
<Settings>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
</Settings>
<Triggers>
<BootTrigger />
</Triggers>
<Actions Context="Author">
<Exec>
<Command>{{ $execute }}</Command>
<Arguments>{{ $argument }}</Arguments>
</Exec>
</Actions>
</Task>
'@
$computerStartXmlParams = @{
'taskPath' = $cronFolderPath
'taskName' = 'Computer Startup'
'description' = 'Runs all scripts under "%PROGRAMDATA%\Scheduled Scripts\Tasks\Startup\*.ps1" every time this computer starts up with the current operating system. No user needs to be logged on. This task will run on both battery and AC power.'
'execute' = 'wscript.exe'
'argument' = '"{0}" Startup' -f $cronVbsLauncher
'creationTimestamp' = Get-Date -Format 'yyyy-MM-ddTHH:mm:ss'
}
$computerStartXmlParams.Keys | ForEach-Object {
$computerStartXml = $computerStartXml.Replace('{{ $' + $_ + ' }}', $computerStartXmlParams."$_")
}
$taskItem = $comService.NewTask(0)
$taskItem.XmlText = $computerStartXml
$baseFolder.RegisterTaskDefinition($computerStartXmlParams.taskName, $taskItem, 6, $null, $null, 5, $null)
# --- CREATE LOGS ---
$existingLogs = Get-EventLog -List | select -expand Log
$requiredLogs = @{
'Scheduled Scripts' = @{
'Source' = @('Daily', 'Monthly', 'Weekly')
}
'Host Access' = @{
'Source' = @('Startup', 'Shutdown', 'Logon', 'Logout', 'UserOOBE')
}
}
$requiredLogs.Keys | ForEach-Object {
if ($_ -notin $existingLogs)
{
$createLogName = $_
New-EventLog -LogName $createLogName -Source $requiredLogs."$createLogName".Source
}
else
{
Write-Warning "Log $_ already exists. If you encounter logging problems with sources, remove them manually and install again."
}
}
# --- WRITE FILES ---
if (Test-Path $cronScriptPath -PathType Leaf)
{
del $cronScriptPath -Force
}
if (-not (Test-Path $cronScriptPath))
{
md $cronScriptPath -Force | Out-Null
}
copy "$PSScriptRoot\src\*" "$cronScriptPath\" -Recurse
@('Daily', 'Weekly', 'Monthly') | ForEach-Object {
if (-not (Test-Path (Join-Path $cronScriptPath -ChildPath "Tasks\$_")))
{
md (Join-Path $cronScriptPath -ChildPath "Tasks\$_") -Force | Out-Null
}
if (-not (Test-Path (Join-Path $cronScriptPath -ChildPath "Tasks\Battery Exempted\$_")))
{
md (Join-Path $cronScriptPath -ChildPath "Tasks\Battery Exempted\$_") -Force | Out-Null
}
}
$sddl = 'O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464G:S-1-5-21-3743141187-1647405133-3778558433-513D:PAI(A;OICIIO;FA;;;CO)(A;OICI;FA;;;SY)(A;OICI;0x1200a9;;;NS)(A;OICI;FA;;;BA)(A;OICI;0x1200a9;;;BU)'
$acl = Get-Acl -Path $cronScriptPath
$acl.SetSecurityDescriptorSddlForm($sddl)
$acl | Set-Acl -Path $cronScriptPath
dir $cronScriptPath -File -Recurse | ForEach-Object { $_ | Unblock-File }