Refactor UAC enablement

This commit is contained in:
Manuel Thalmann 2023-06-29 04:05:12 +02:00
parent 35cafdacef
commit cba6e514bb

View file

@ -1,6 +1,6 @@
. "$PSScriptRoot/../Scripts/Context.ps1"; . "$PSScriptRoot/../Scripts/Context.ps1";
$uacDisablerName = "PortValhalla UAC Disabler"; $uacDisablerName = "PortValhalla UAC Disabler";
$cleanupName = "PortValhalla Cleanup"; $taskTrigger = 1337;
function New-PersonalUser([Context] $context) function New-PersonalUser([Context] $context)
{ {
@ -42,21 +42,24 @@ function New-PersonalUser([Context] $context)
Restart-Computer; Restart-Computer;
exit; exit;
} }
elseif ((Get-ScheduledTask $uacDisablerName)) elseif ((Get-UACState))
{ {
while ((Get-ScheduledTask $uacDisablerName) -ne "Ready") Write-EventLog -LogName Application -Source "Application" -EventId $taskTrigger -Message "This event was created by $env:Username";
for ($i = 0; $i -lt $120; $i++)
{ {
Start-Sleep 1; if ((Get-UACState))
{
break;
}
} }
$taskInfo = Get-ScheduledTask $uacDisablerName; if ((Get-UACState))
if ($taskInfo.LastTaskResult -ne 0)
{ {
Write-Error "Disabling UAC was unsuccessful."; Write-Error "UAC Could not be disabled!";
Read-Host "Press enter to continue anyway";
} }
Start-ScheduledTask $cleanupName;
Enable-CurrentUserAutologon $context; Enable-CurrentUserAutologon $context;
Write-Information "Removing Admin Account"; Write-Information "Removing Admin Account";
@ -84,6 +87,7 @@ function Set-UACState([bool] $value) {
function Enable-UACNextLogin([Context] $context) { function Enable-UACNextLogin([Context] $context) {
Set-UACState $true; Set-UACState $true;
$tempTask = "PortValhalla Temp";
$key = Get-SystemPolicyKey; $key = Get-SystemPolicyKey;
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument ( $action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument (
@ -91,7 +95,7 @@ function Enable-UACNextLogin([Context] $context) {
" ", " ",
@( @(
"-c", "-c",
"Set-ItemProperty '$($key.PSPath)' -Name 'EnableLUA' -Value 0;", "Set-ItemProperty '$($key.PSPath)' -Name 'EnableLUA' -Value 0 -Type DWord;",
"Unregister-ScheduledTask -Confirm:`$false '$uacDisablerName';", "Unregister-ScheduledTask -Confirm:`$false '$uacDisablerName';",
". '$PSScriptRoot/../Scripts/Context.ps1';", ". '$PSScriptRoot/../Scripts/Context.ps1';",
"`$context = [Context]::new();", "`$context = [Context]::new();",
@ -99,10 +103,12 @@ function Enable-UACNextLogin([Context] $context) {
"`$context.RegisterReboot((Get-Item 'Registry::HKEY_USERS\$((Get-LocalUser $context.UserName).SID)'));", "`$context.RegisterReboot((Get-Item 'Registry::HKEY_USERS\$((Get-LocalUser $context.UserName).SID)'));",
"Restart-Computer -Force;"))); "Restart-Computer -Force;")));
$trigger = New-ScheduledTaskTrigger -AtLogOn; schtasks /Create /SC ONEVENT /MO "*[System[Provider[@Name='Application'] and EventID=$taskTrigger]]" /TR cmd.exe /TN "$tempTask";
$trigger = (Get-ScheduledTask $tempTask).Triggers;
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest; $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest;
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger; $task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger;
$null = Register-ScheduledTask $uacDisablerName -InputObject $task; $null = Register-ScheduledTask $uacDisablerName -InputObject $task;
$null = Unregister-ScheduledTask -Confirm:$false $tempTask;
} }
function Enable-CurrentUserAutologon([Context] $context) function Enable-CurrentUserAutologon([Context] $context)