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";
$uacDisablerName = "PortValhalla UAC Disabler";
$cleanupName = "PortValhalla Cleanup";
$taskTrigger = 1337;
function New-PersonalUser([Context] $context)
{
@ -42,21 +42,24 @@ function New-PersonalUser([Context] $context)
Restart-Computer;
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 ($taskInfo.LastTaskResult -ne 0)
if ((Get-UACState))
{
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;
Write-Information "Removing Admin Account";
@ -84,6 +87,7 @@ function Set-UACState([bool] $value) {
function Enable-UACNextLogin([Context] $context) {
Set-UACState $true;
$tempTask = "PortValhalla Temp";
$key = Get-SystemPolicyKey;
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument (
@ -91,7 +95,7 @@ function Enable-UACNextLogin([Context] $context) {
" ",
@(
"-c",
"Set-ItemProperty '$($key.PSPath)' -Name 'EnableLUA' -Value 0;",
"Set-ItemProperty '$($key.PSPath)' -Name 'EnableLUA' -Value 0 -Type DWord;",
"Unregister-ScheduledTask -Confirm:`$false '$uacDisablerName';",
". '$PSScriptRoot/../Scripts/Context.ps1';",
"`$context = [Context]::new();",
@ -99,10 +103,12 @@ function Enable-UACNextLogin([Context] $context) {
"`$context.RegisterReboot((Get-Item 'Registry::HKEY_USERS\$((Get-LocalUser $context.UserName).SID)'));",
"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;
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger;
$null = Register-ScheduledTask $uacDisablerName -InputObject $task;
$null = Unregister-ScheduledTask -Confirm:$false $tempTask;
}
function Enable-CurrentUserAutologon([Context] $context)