Allow registering multiple scheduled tasks

This commit is contained in:
Manuel Thalmann 2023-06-29 19:40:54 +02:00
parent 8606cd441a
commit 736d25f1a8

View file

@ -75,17 +75,16 @@ function Set-UACState([bool] $value) {
function Enable-UACNextLogin([Context] $context) {
Set-UACState $true;
$tempTask = "PortValhalla Temp";
$uacDisablerName = "PortValhalla UAC Disabler";
$uacDisablerTrigger = Get-Random;
$key = Get-SystemPolicyKey;
$context.Set($uacDisablerTriggerProperty, $uacDisablerTrigger, "DWord");
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument (
[string]::Join(
" ",
$optionCollection = [System.Tuple[uint, string, string[]][]]@(
[System.Tuple]::Create(
$uacDisablerTrigger,
"PortValhalla UAC Disabler",
@(
"-c",
"Set-ItemProperty '$($key.PSPath)' -Name 'EnableLUA' -Value 0 -Type DWord;",
"Unregister-ScheduledTask -Confirm:`$false '$uacDisablerName';",
". '$PSScriptRoot/../Scripts/Context.ps1';",
@ -93,12 +92,21 @@ function Enable-UACNextLogin([Context] $context) {
"`$context.DeregisterNewUserReboot();",
"Restart-Computer -Force;")));
schtasks /Create /SC ONEVENT /EC Application /MO "*[System[Provider[@Name='Application'] and EventID=$uacDisablerTrigger]]" /TR cmd.exe /TN "$tempTask";
$trigger = (Get-ScheduledTask $tempTask).Triggers;
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest;
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger;
$null = Register-ScheduledTask $uacDisablerName -InputObject $task;
$null = Unregister-ScheduledTask -Confirm:$false $tempTask;
foreach ($options in $optionCollection) {
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument (
[string]::Join(
" ",
(
@("-c") +
($options.Item3 | ForEach-Object { $_.TrimEnd(";") + ";" }))));
schtasks /Create /SC ONEVENT /EC Application /MO "*[System[Provider[@Name='Application'] and EventID=$($options.Item1)]]" /TR cmd.exe /TN "$tempTask";
$trigger = (Get-ScheduledTask $tempTask).Triggers;
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest;
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger;
$null = Register-ScheduledTask $options.Item2 -InputObject $task;
$null = Unregister-ScheduledTask -Confirm:$false $tempTask;
}
}
function Enable-PersonalUserAutologon([Context] $context)