Add a method for cleaning up artifacts during setup

This commit is contained in:
Manuel Thalmann 2023-06-28 22:28:22 +02:00
parent bf59f2e3a3
commit f791ec1eff

View file

@ -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;
}