-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppControlTask.au3
More file actions
630 lines (520 loc) · 30.7 KB
/
AppControlTask.au3
File metadata and controls
630 lines (520 loc) · 30.7 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
; *** Start added Standard Include files by AutoIt3Wrapper ***
#include <Date.au3>
#include <FontConstants.au3>
#include <WinAPISysInternals.au3>
#include <WinAPIInternals.au3>
#include <WindowsConstants.au3>
#include <WinAPIFiles.au3>
#include <AutoItConstants.au3>
; *** End added Standard Include files by AutoIt3Wrapper ***
#Region
#include <MsgBoxConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>
#include <StringConstants.au3>
#include <APISysConstants.au3>
#EndRegion
#include "includes\TaskScheduler.au3"
#include "includes\libnotif.au3"
#include "includes\GUIDarkMode_v0.02mod.au3"
#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=AppControl.ico
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Description=App Control Task Manager
#AutoIt3Wrapper_Res_Fileversion=6.0.3
#AutoIt3Wrapper_Res_ProductVersion=6.0.3
#AutoIt3Wrapper_Res_ProductName=AppControlTaskManager
#AutoIt3Wrapper_Outfile_x64=AppControlTask.exe
#AutoIt3Wrapper_Res_LegalCopyright=@ 2025 WildByDesign
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_HiDpi=P
#AutoIt3Wrapper_Res_Icon_Add=AppControl.ico
#AutoIt3Wrapper_UseUpx=N
#AutoIt3Wrapper_Compression=0
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
$sTitle = "AppControlTaskHelper"
Global $MainFont
Global $ifPS7Exists = FileExists(@ProgramFilesDir & '\PowerShell\7\pwsh.exe')
If $ifPS7Exists Then
Global $o_powershell = @ProgramFilesDir & '\PowerShell\7\pwsh.exe -NoProfile -Command'
Else
Global $o_powershell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command"
EndIf
If @Compiled = 0 Then
; System aware DPI awareness
;DllCall("User32.dll", "bool", "SetProcessDPIAware")
; Per-monitor V2 DPI awareness
DllCall("User32.dll", "bool", "SetProcessDpiAwarenessContext" , "HWND", "DPI_AWARENESS_CONTEXT" -4)
EndIf
Local Const $sSegUIVar = @WindowsDir & "\fonts\SegUIVar.ttf"
Local $SegUIVarExists = FileExists($sSegUIVar)
If $SegUIVarExists Then
Global $MainFont = "Segoe UI Variable Display"
GUISetFont(8.5, $FW_NORMAL, -1, $MainFont)
Else
Global $MainFont = "Segoe UI"
GUISetFont(8.5, $FW_NORMAL, -1, $MainFont)
EndIf
;Global $isDarkMode = _WinAPI_ShouldAppsUseDarkMode()
;---
; configure library
_LibNotif_Config($LIBNOTIF_CFG_TIMEOUT, 10000)
_LibNotif_Config($LIBNOTIF_CFG_MINTOASTWIDTH, 128)
;_LibNotif_Config($LIBNOTIF_CFG_POSITION, $LIBNOTIF_POS_UR)
_LibNotif_Config($LIBNOTIF_CFG_MAXTEXTWIDTH, @DesktopWidth - 100)
_LibNotif_Config($LIBNOTIF_CFG_CLOSEBUTTON, True)
$idLightBk = _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_BTNFACE))
If $isDarkMode = True Then
_LibNotif_Config($LIBNOTIF_CFG_BKCOLOR, 0x202020)
_LibNotif_Config($LIBNOTIF_CFG_TEXTCOLOR, 0xe0e0e0)
Else
_LibNotif_Config($LIBNOTIF_CFG_BKCOLOR, $idLightBk)
_LibNotif_Config($LIBNOTIF_CFG_TEXTCOLOR, 0x000000)
EndIf
_LibNotif_Config($LIBNOTIF_CFG_TEXTFONTNAME, $MainFont)
_LibNotif_Config($LIBNOTIF_CFG_TEXTSIZE, 10)
_LibNotif_Config($LIBNOTIF_CFG_TEXTWEIGHT, 400)
_LibNotif_Config($LIBNOTIF_CFG_TEXTATTRIBUTES, 0)
_LibNotif_Config($LIBNOTIF_CFG_TEXTMARGIN, 15)
_LibNotif_Config($LIBNOTIF_CFG_BUTTONSIZE, 30)
;_LibNotif_Config($LIBNOTIF_CFG_SHOW_ANIMSTYLE, $AW_VER_NEGATIVE)
;_LibNotif_Config($LIBNOTIF_CFG_HIDE_ANIMSTYLE, $AW_VER_POSITIVE + $AW_HIDE)
; init library
_LibNotif_Init()
;---
Func task_create_folder()
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
Global $sFolder = "\AppControlTray"
Global $aTaskProperties = _TS_FolderCreate($oService, $sFolder)
_TS_Close($oService)
Endfunc
Func task_create_blocked()
Global $sFolder = "\AppControlTray" ; Folder where to create the task
Global $sName = "ToastBlocked" ; Name of the task to create
; *****************************************************************************
; Prepare start and end date of the trigger. Format must be YYYY-MM-DDTHH:MM:SS
; *****************************************************************************
Global $sStartDateTime = _DateAdd("n", 2, _NowCalc())
$sStartDateTime = StringReplace($sStartDateTime, "/", "-")
$sStartDateTime = StringReplace($sStartDateTime, " ", "T")
Global $sEndDateTime = _DateAdd("M", 4, _NowCalc())
$sEndDateTime = StringReplace($sEndDateTime, "/", "-")
$sEndDateTime = StringReplace($sEndDateTime, " ", "T")
; *****************************************************************************
; Connect to the Task Scheduler Service
; *****************************************************************************
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Delete a task in the same folder with the same name
; *****************************************************************************
_TS_TaskDelete($oService, $sFolder & "\" & $sName)
; If @error <> 0 And @error <> 2 Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskDelete returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Create a new task
; *****************************************************************************
; Create the Task Definition object
Global $oTaskDefinition = _TS_TaskCreate($oService)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskCreate returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Set all task properties
Global $aProperties[] = [ _
"ACTIONS|Path|AppControlTask.exe", _
"SETTINGS|DisallowStartIfOnBatteries|False", _
"SETTINGS|StopIfGoingOnBatteries|False", _
"SETTINGS|Compatibility|4", _
"SETTINGS|MultipleInstances|0", _
"SETTINGS|ExecutionTimeLimit|PT0S", _
"REGISTRATIONINFO|Author|" & @ComputerName & "\" & @UserName, _
"REGISTRATIONINFO|Description|Task that triggers a toast notification on App Control block events.", _
"ACTIONS|WorkingDirectory|" & @ScriptDir, _
"ACTIONS|Arguments|blocked", _
"TRIGGERS|Enabled|True", _
"TRIGGERS|Type|" & $TASK_TRIGGER_EVENT, _
"TRIGGERS|Subscription|<QueryList><Query Id='0' Path='Microsoft-Windows-CodeIntegrity/Operational'><Select Path='Microsoft-Windows-CodeIntegrity/Operational'>*[System[(EventID=3077)]]</Select></Query></QueryList>" _
]
_TS_TaskPropertiesSet($oTaskDefinition, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the TaskDefinition returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Create an Event trigger
Global $oTrigger = _TS_TriggerCreate($oTaskDefinition, $TASK_TRIGGER_EVENT)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "Creating the Trigger returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
_TS_TaskPropertiesSet($oTrigger, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the Trigger returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Create an Action
Global $oAction = _TS_ActionCreate($oTaskDefinition, $TASK_ACTION_EXEC)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "Creating the Action returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
_TS_TaskPropertiesSet($oAction, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the Action returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; List properties of the Task Definition
Global $aTaskProperties = _TS_TaskPropertiesGet($oService, $oTaskDefinition,1 , True)
If Not @error Then
;_ArrayDisplay($aTaskProperties, "Properties of the task to be created")
Else
MsgBox($MB_ICONERROR, "_TS_TaskPropertiesGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf
; Register the task
Global $oTask = _TS_TaskRegister($oService, $sFolder, $sName, $oTaskDefinition)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskRegister returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
;MsgBox($MB_ICONINFORMATION, "_TS_TaskCreate", "Task " & $sName & " has been created!")
_TS_Close($oService)
Endfunc
Func task_create_audit()
Global $sFolder = "\AppControlTray" ; Folder where to create the task
Global $sName = "ToastAudit" ; Name of the task to create
; *****************************************************************************
; Prepare start and end date of the trigger. Format must be YYYY-MM-DDTHH:MM:SS
; *****************************************************************************
Global $sStartDateTime = _DateAdd("n", 2, _NowCalc())
$sStartDateTime = StringReplace($sStartDateTime, "/", "-")
$sStartDateTime = StringReplace($sStartDateTime, " ", "T")
Global $sEndDateTime = _DateAdd("M", 4, _NowCalc())
$sEndDateTime = StringReplace($sEndDateTime, "/", "-")
$sEndDateTime = StringReplace($sEndDateTime, " ", "T")
; *****************************************************************************
; Connect to the Task Scheduler Service
; *****************************************************************************
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Delete a task in the same folder with the same name
; *****************************************************************************
_TS_TaskDelete($oService, $sFolder & "\" & $sName)
; If @error <> 0 And @error <> 2 Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskDelete returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Create a new task
; *****************************************************************************
; Create the Task Definition object
Global $oTaskDefinition = _TS_TaskCreate($oService)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskCreate returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Set all task properties
Global $aProperties[] = [ _
"ACTIONS|Path|AppControlTask.exe", _
"SETTINGS|DisallowStartIfOnBatteries|False", _
"SETTINGS|StopIfGoingOnBatteries|False", _
"SETTINGS|Compatibility|4", _
"SETTINGS|MultipleInstances|0", _
"SETTINGS|ExecutionTimeLimit|PT0S", _
"REGISTRATIONINFO|Author|" & @ComputerName & "\" & @UserName, _
"REGISTRATIONINFO|Description|Task that triggers a toast notification on App Control audit events.", _
"ACTIONS|WorkingDirectory|" & @ScriptDir, _
"ACTIONS|Arguments|audit", _
"TRIGGERS|Enabled|True", _
"TRIGGERS|Type|" & $TASK_TRIGGER_EVENT, _
"TRIGGERS|Subscription|<QueryList><Query Id='0' Path='Microsoft-Windows-CodeIntegrity/Operational'><Select Path='Microsoft-Windows-CodeIntegrity/Operational'>*[System[(EventID=3076)]]</Select></Query></QueryList>" _
]
_TS_TaskPropertiesSet($oTaskDefinition, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the TaskDefinition returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Create an Event trigger
Global $oTrigger = _TS_TriggerCreate($oTaskDefinition, $TASK_TRIGGER_EVENT)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "Creating the Trigger returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
_TS_TaskPropertiesSet($oTrigger, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the Trigger returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Create an Action
Global $oAction = _TS_ActionCreate($oTaskDefinition, $TASK_ACTION_EXEC)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "Creating the Action returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
_TS_TaskPropertiesSet($oAction, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the Action returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; List properties of the Task Definition
Global $aTaskProperties = _TS_TaskPropertiesGet($oService, $oTaskDefinition,1 , True)
If Not @error Then
;_ArrayDisplay($aTaskProperties, "Properties of the task to be created")
Else
MsgBox($MB_ICONERROR, "_TS_TaskPropertiesGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf
; Register the task
Global $oTask = _TS_TaskRegister($oService, $sFolder, $sName, $oTaskDefinition)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskRegister returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
;MsgBox($MB_ICONINFORMATION, "_TS_TaskCreate", "Task " & $sName & " has been created!")
_TS_Close($oService)
Endfunc
Func task_create_refresh()
Global $sFolder = "\AppControlTray" ; Folder where to create the task
Global $sName = "PolicyRefresh" ; Name of the task to create
; *****************************************************************************
; Prepare start and end date of the trigger. Format must be YYYY-MM-DDTHH:MM:SS
; *****************************************************************************
Global $sStartDateTime = _DateAdd("n", 2, _NowCalc())
$sStartDateTime = StringReplace($sStartDateTime, "/", "-")
$sStartDateTime = StringReplace($sStartDateTime, " ", "T")
Global $sEndDateTime = _DateAdd("M", 4, _NowCalc())
$sEndDateTime = StringReplace($sEndDateTime, "/", "-")
$sEndDateTime = StringReplace($sEndDateTime, " ", "T")
; *****************************************************************************
; Connect to the Task Scheduler Service
; *****************************************************************************
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Delete a task in the same folder with the same name
; *****************************************************************************
_TS_TaskDelete($oService, $sFolder & "\" & $sName)
; If @error <> 0 And @error <> 2 Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskDelete returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Create a new task
; *****************************************************************************
; Create the Task Definition object
Global $oTaskDefinition = _TS_TaskCreate($oService)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskCreate returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Set all task properties
Global $aProperties[] = [ _
"ACTIONS|Path|AppControlTask.exe", _
"SETTINGS|DisallowStartIfOnBatteries|False", _
"SETTINGS|StopIfGoingOnBatteries|False", _
"SETTINGS|Compatibility|4", _
"SETTINGS|MultipleInstances|0", _
"SETTINGS|ExecutionTimeLimit|PT0S", _
"REGISTRATIONINFO|Author|" & @ComputerName & "\" & @UserName, _
"REGISTRATIONINFO|Description|Task that triggers a toast notification on App Control policy refresh events.", _
"ACTIONS|WorkingDirectory|" & @ScriptDir, _
"ACTIONS|Arguments|refresh", _
"TRIGGERS|Enabled|True", _
"TRIGGERS|Type|" & $TASK_TRIGGER_EVENT, _
"TRIGGERS|Subscription|<QueryList><Query Id='0' Path='Microsoft-Windows-CodeIntegrity/Operational'><Select Path='Microsoft-Windows-CodeIntegrity/Operational'>*[System[(EventID=3102)]]</Select></Query></QueryList>" _
]
_TS_TaskPropertiesSet($oTaskDefinition, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the TaskDefinition returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Create an Event trigger
Global $oTrigger = _TS_TriggerCreate($oTaskDefinition, $TASK_TRIGGER_EVENT)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "Creating the Trigger returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
_TS_TaskPropertiesSet($oTrigger, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the Trigger returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; Create an Action
Global $oAction = _TS_ActionCreate($oTaskDefinition, $TASK_ACTION_EXEC)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "Creating the Action returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
_TS_TaskPropertiesSet($oAction, $aProperties)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskPropertiesSet for the Action returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; List properties of the Task Definition
Global $aTaskProperties = _TS_TaskPropertiesGet($oService, $oTaskDefinition,1 , True)
If Not @error Then
;_ArrayDisplay($aTaskProperties, "Properties of the task to be created")
Else
MsgBox($MB_ICONERROR, "_TS_TaskPropertiesGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf
; Register the task
Global $oTask = _TS_TaskRegister($oService, $sFolder, $sName, $oTaskDefinition)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskCreate", "_TS_TaskRegister returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
;MsgBox($MB_ICONINFORMATION, "_TS_TaskCreate", "Task " & $sName & " has been created!")
_TS_Close($oService)
Endfunc
Func task_delete_blocked()
; *****************************************************************************
; Connect to the Task Scheduler Service
; *****************************************************************************
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Delete task "Test-Task" from folder "\Test"
; *****************************************************************************
Global $sTaskPath = "\AppControlTray\ToastBlocked" ; Folder and name of the task to be deleted
_TS_TaskDelete($oService, $sTaskPath)
If Not @error Then
;MsgBox($MB_ICONINFORMATION, "_TS_TaskDelete", "Task '" & $sTaskPath & "' successfully deleted!")
Else
MsgBox($MB_ICONERROR, "_TS_TaskDelete", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf
_TS_Close($oService)
Endfunc
Func task_delete_audit()
; *****************************************************************************
; Connect to the Task Scheduler Service
; *****************************************************************************
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Delete task "Test-Task" from folder "\Test"
; *****************************************************************************
Global $sTaskPath = "\AppControlTray\ToastAudit" ; Folder and name of the task to be deleted
_TS_TaskDelete($oService, $sTaskPath)
If Not @error Then
;MsgBox($MB_ICONINFORMATION, "_TS_TaskDelete", "Task '" & $sTaskPath & "' successfully deleted!")
Else
MsgBox($MB_ICONERROR, "_TS_TaskDelete", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf
_TS_Close($oService)
Endfunc
Func task_delete_refresh()
; *****************************************************************************
; Connect to the Task Scheduler Service
; *****************************************************************************
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Delete task "Test-Task" from folder "\Test"
; *****************************************************************************
Global $sTaskPath = "\AppControlTray\PolicyRefresh" ; Folder and name of the task to be deleted
_TS_TaskDelete($oService, $sTaskPath)
If Not @error Then
;MsgBox($MB_ICONINFORMATION, "_TS_TaskDelete", "Task '" & $sTaskPath & "' successfully deleted!")
Else
MsgBox($MB_ICONERROR, "_TS_TaskDelete", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf
_TS_Close($oService)
Endfunc
Func task_delete_folder()
; *****************************************************************************
; Connect to the Task Scheduler Service
; *****************************************************************************
Global $oService = _TS_Open()
If @error <> 0 Then Exit MsgBox($MB_ICONERROR, "Task Scheduler UDF", "Error connecting to the Task Scheduler Service. @error = " & @error & ", @extended = " & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
; *****************************************************************************
; Delete a folder
; $sFolder has always to start at the root folder (means you have to
; specify all the folders from the root down even when they already exist)
; *****************************************************************************
Global $sFolder = "\AppControlTray"
_TS_FolderDelete($oService, $sFolder)
If Not @error Then
;MsgBox($MB_ICONINFORMATION, "_TS_FolderDelete", "Folder: " & $sFolder & " successfully deleted!")
Else
MsgBox($MB_ICONERROR, "_TS_FolderDelete", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf
_TS_Close($oService)
Endfunc
If $CmdLine[0] = 0 Then Exit MsgBox(16, $sTitle, "No parameters passed!")
If $CmdLine[1] = "blocked" Then
$sLastBlocked = GetLastBlocked()
$iRegWrite = RegWrite("HKEY_CURRENT_USER\Software\AppControlTray", "Blocked", "REG_DWORD", "1")
$sText = "An application was stopped from running." & @CRLF & @CRLF & "For security and stability reasons, unknown applications" & @CRLF & "are prevented from running on this device." & @CRLF & @CRLF & 'Blocked App:' & @CRLF & @CRLF & $sLastBlocked
_LibNotif_Notif($sText)
; wait until notification is closed
While _LibNotif_IsVisible()
Sleep(100)
WEnd
EndIf
If $CmdLine[1] = "audit" Then
$sLastAudit = GetLastAudit()
$sText = "An application would have been stopped from running." & @CRLF & @CRLF & "Unknown applications are prevented from running. However," & @CRLF & "this was able to run due to Audit Mode." & @CRLF & @CRLF & 'Blocked App:' & @CRLF & @CRLF & $sLastAudit
_LibNotif_Notif($sText)
; wait until notification is closed
While _LibNotif_IsVisible()
Sleep(100)
WEnd
EndIf
If $CmdLine[1] = "refresh" Then
$sText = "Your App Control policies have been refreshed." & @CRLF & @CRLF & "Code Integrity policy refresh finished successfully."
_LibNotif_Notif($sText)
; wait until notification is closed
While _LibNotif_IsVisible()
Sleep(100)
WEnd
EndIf
If $CmdLine[1] = "convert" Then
$mFile = FileOpenDialog("Select XML Policy File for Conversion", @ScriptDir, "Policy Files (*.xml)", 1)
If @error Then
Exit
Else
Local $sFileRead = FileRead($mFile)
$aVersionEx = _StringBetween($sFileRead, "<VersionEx>", "</VersionEx>")
$aExtract = _StringBetween($sFileRead, "<PolicyID>", "</PolicyID>")
Local $versionsplit = StringSplit($aVersionEx[0], '.')
;MsgBox($MB_SYSTEMMODAL, "version", $versionsplit[1] & '.' & $versionsplit[2] & '.' & $versionsplit[3] & '.' & $versionsplit[4])
Local $versionplus = $versionsplit[4] + 1
Local $versionincreased = $versionsplit[1] & '.' & $versionsplit[2] & '.' & $versionsplit[3] & '.' & $versionplus
;MsgBox($MB_SYSTEMMODAL, "version increased", $versionincreased)
Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
Local $aPathSplit = _PathSplit($mFile, $sDrive, $sDir, $sFileName, $sExtension)
Local $xmlfiledir = $sDrive & $sDir
Local $xmlfilenew = $sFileName
Local $xmlfilename2 = StringInStr($xmlfilenew, "-v")
Local $aDays = StringSplit($xmlfilenew, "-v", 1)
Local $xmlfilename3 = $aDays[1]
Local $xmlupdatedname = $sDrive & $sDir & $aDays[1] & '-v' & $versionincreased & '.xml'
;MsgBox($MB_SYSTEMMODAL, "Title", $xmlupdatedname)
FileCopy($mFile, $xmlupdatedname, $FC_OVERWRITE)
Local $binarysave = $sFileName & '.cip'
Local $binaryname = $xmlfiledir & $sFileName & '.cip'
Local $binarynameGUIDsave = $aExtract[0] & '.cip'
Local $binarynameGUID = $xmlfiledir & $aExtract[0] & '.cip'
EndIf
Local Const $sMessage = "Choose a filename for saving your policy binary"
Local $sFileSaveDialog = FileSaveDialog($sMessage, "::{450D8FBA-AD25-11D0-98A8-0800361B1103}", "Policy Files (*.cip)", $FD_PATHMUSTEXIST+$FD_PROMPTOVERWRITE, $binarynameGUIDsave)
If @error Then
Else
Local $sFileName = StringTrimLeft($sFileSaveDialog, StringInStr($sFileSaveDialog, "\", $STR_NOCASESENSEBASIC, -1))
Local $iExtension = StringInStr($sFileName, ".", $STR_NOCASESENSEBASIC)
If $iExtension Then
If Not (StringTrimLeft($sFileName, $iExtension - 1) = ".cip") Then $sFileSaveDialog &= ".cip"
Else
$sFileSaveDialog &= ".cip"
EndIf
EndIf
Local $bDrive = "", $bDir = "", $bFileName = "", $bExtension = ""
Local $bPathSplit = _PathSplit($sFileSaveDialog, $bDrive, $bDir, $bFileName, $bExtension)
Local $xmlfiledir2 = $bDrive & $bDir
Local $binarynameonly = $bFileName & $bExtension
Local $binarynamechosen = $sFileSaveDialog
; .\Convert-Policy.ps1 -XmlPolicyFile $mFile -BinaryDir $xmlfiledir -XmlOutName $xmlfilenew -BinaryFile $binarynameonly
; Set-CIPolicyVersion -FilePath $XmlPolicyFileNew -Version $PolicyVersionNew
; ConvertFrom-CIPolicy -XmlFilePath $XmlPolicyFileNew -BinaryFilePath "$BinaryDir\$BinaryFile"
Local $quote = "'"
Local $quote2 = '"'
Local $cmd1 = ' -NoProfile -Command ' & $quote2 & '$CurrentDate = Get-Date -UFormat "%Y-%m-%d"; Set-CIPolicyIdInfo -FilePath ' & $quote & $xmlupdatedname & $quote & ' -PolicyId $CurrentDate; Set-CIPolicyVersion -FilePath ' & $quote & $xmlupdatedname & $quote & ' -Version ' & $quote & $versionincreased & $quote & '; ConvertFrom-CIPolicy -XmlFilePath ' & $quote & $xmlupdatedname & $quote & ' -BinaryFilePath ' & $quote & $binarynamechosen & $quote & $quote2
Local $o_powershell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Local $o_Pid1 = Run($o_powershell & $cmd1, @ScriptDir, @SW_Hide)
EndIf
If $CmdLine[1] = "installtasks" Then
task_create_folder()
Sleep(1000)
task_create_blocked()
task_create_audit()
task_create_refresh()
EndIf
If $CmdLine[1] = "removetasks" Then
task_delete_blocked()
task_delete_audit()
task_delete_refresh()
Sleep(1000)
task_delete_folder()
EndIf
Func GetLastAudit()
Local $o_CmdString1 = " (get-winevent -LogName Microsoft-Windows-CodeIntegrity/Operational -FilterXPath '*[System[(EventID = 3076) and (EventRecordID > 0)]]') | foreach {$_.Properties[1]}"
Local $o_Pid = Run($o_powershell & $o_CmdString1 , "", @SW_Hide, $STDOUT_CHILD)
ProcessWaitCloseEx($o_Pid)
$out = StdoutRead($o_Pid)
$sString = StringReplace($out, "[32;1mValue[0m", "")
$sString = StringReplace($sString, "[32;1m-----[0m", "")
$sString = StringReplace($sString, "Value", "")
$sString = StringReplace($sString, "-----", "")
$sString = StringStripWS($sString, $STR_STRIPLEADING + $STR_STRIPTRAILING)
$aDays = StringSplit($sString, @CR)
$sLastAudit = $aDays[1]
$sLastAudit = _DosPathNameToPathName($sLastAudit)
Return $sLastAudit
EndFunc
Func GetLastBlocked()
Local $o_CmdString1 = " (get-winevent -LogName Microsoft-Windows-CodeIntegrity/Operational -FilterXPath '*[System[(EventID = 3077) and (EventRecordID > 0)]]') | foreach {$_.Properties[1]}"
Local $o_Pid = Run($o_powershell & $o_CmdString1 , "", @SW_Hide, $STDOUT_CHILD)
ProcessWaitCloseEx($o_Pid)
$out = StdoutRead($o_Pid)
$sString = StringReplace($out, "[32;1mValue[0m", "")
$sString = StringReplace($sString, "[32;1m-----[0m", "")
$sString = StringReplace($sString, "Value", "")
$sString = StringReplace($sString, "-----", "")
$sString = StringStripWS($sString, $STR_STRIPLEADING + $STR_STRIPTRAILING)
$aDays = StringSplit($sString, @CR)
$sLastBlocked = $aDays[1]
$sLastBlocked = _DosPathNameToPathName($sLastBlocked)
Return $sLastBlocked
EndFunc
Func ProcessWaitCloseEx($iPID)
While ProcessExists($iPID) And Sleep(10)
WEnd
EndFunc
Func _DosPathNameToPathName($sPath)
Local $sName, $aDrive = DriveGetDrive('ALL')
If Not IsArray($aDrive) Then
Return SetError(1, 0, $sPath)
EndIf
For $i = 1 To $aDrive[0]
$sName = _WinAPI_QueryDosDevice($aDrive[$i])
If StringInStr($sPath, $sName) = 1 Then
Return StringReplace($sPath, $sName, StringUpper($aDrive[$i]), 1)
EndIf
Next
Return SetError(2, 0, $sPath)
EndFunc