From f791ec1effc42651c87b2faa7f4635c697e3b954 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 28 Jun 2023 22:28:22 +0200 Subject: [PATCH] Add a method for cleaning up artifacts during setup --- scripts/Windows/OS/User.ps1 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/scripts/Windows/OS/User.ps1 b/scripts/Windows/OS/User.ps1 index 184157e2..e3031d21 100644 --- a/scripts/Windows/OS/User.ps1 +++ b/scripts/Windows/OS/User.ps1 @@ -1,5 +1,6 @@ . "$PSScriptRoot/../Scripts/Context.ps1"; $uacDisablerName = "PortValhalla UAC Disabler"; +$cleanupName = "PortValhalla Cleanup"; function New-PersonalUser([Context] $context) { @@ -44,6 +45,22 @@ function New-PersonalUser([Context] $context) Restart-Computer; exit; } + elseif ((Get-ScheduledTask $uacDisablerName)) + { + while ((Get-ScheduledTask $uacDisablerName) -ne "Ready") + { + Start-Sleep 1; + } + + $taskInfo = Get-ScheduledTask $uacDisablerName; + + if ($taskInfo.LastTaskResult -ne 0) + { + Write-Error "Disabling UAC was unsuccessful."; + } + + Start-ScheduledTask $cleanupName; + } } function Enable-UACNextLogin() { @@ -62,4 +79,15 @@ function Enable-UACNextLogin() { $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest; $task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger; $null = Register-ScheduledTask $uacDisablerName -InputObject $task; + + $action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument ( + [string]::Join( + " ", + @( + "-c", + "Unregister-ScheduledTask -Force $uacDisablerName;", + "Unregister-ScheduledTask -Force $cleanupName;"))); + + $task = New-ScheduledTask -Action $action -Principal $principal; + $null = Register-ScheduledTask $cleanupName -InputObject $task; }