Execute OneShot tasks in any operation

This commit is contained in:
Manuel Thalmann 2024-08-27 14:05:16 +02:00
parent 3032147d2d
commit bbce6be453
3 changed files with 437 additions and 445 deletions

View file

@ -3,8 +3,6 @@ using namespace System.Security.AccessControl;
using namespace System.Security.Principal;
enum SetupStage {
Idle
OneShot
Configure
Install
CreateUser
@ -389,7 +387,7 @@ $null = New-Module {
Gets a value indicating whether the setup has finished.
#>
function Get-IsFinished {
return [bool] (((Get-Stage) -eq ([SetupStage]::Idle)) -or (Get-SetupOption $finishedOption));
return [bool](Get-SetupOption $finishedOption);
}
<#

View file

@ -29,18 +29,24 @@ $null = New-Module {
function Start-Operation {
param(
[switch] $NonInteractive = ((Get-Stage) -eq ([SetupStage]::OneShot)),
[switch] $NonInteractive,
[switch] $NoImplicitCleanup,
[scriptblock] $Action
)
$cleanup = { };
$taskPending = $false;
if (-not $Global:InOperation) {
if ($env:DEBUG) {
Set-PSDebug -Trace 1;
}
if ($null -ne (Get-OneShotTask)) {
$taskPending = $true;
[switch] $NonInteractive = $true;
}
$Global:InOperation = $true;
$Global:ErrorActionPreference = $NonInteractive.IsPresent ? 'Continue' : 'Inquire';
@ -229,7 +235,12 @@ $null = New-Module {
}
}
if ($taskPending) {
Start-OneShot;
} else {
& $Action;
}
& $cleanup;
}
@ -238,7 +249,13 @@ $null = New-Module {
Gets the current OneShot task.
#>
function Get-OneShotTask {
[OneShotTask](Get-SetupOption $taskOption);
$task = Get-SetupOption $taskOption;
if ($task) {
return [OneShotTask]$task;
} else {
return $null;
}
}
<#
@ -291,8 +308,6 @@ $null = New-Module {
[OneShotTask] $Task
)
$currentStage = Get-Stage;
Set-Stage ([SetupStage]::OneShot);
& $taskSetter $Task;
& {
@ -321,8 +336,6 @@ $null = New-Module {
}
};
Set-Stage $currentStage;
if (Test-Path $errorPath) {
$errorMessage = Get-Content $errorPath;
Remove-Item $errorPath;
@ -340,23 +353,27 @@ $null = New-Module {
Executes the specified action and notifies the OneShot task executor.
#>
function Start-OneShot {
param(
[scriptblock] $Action
)
try {
Start-Operation -NonInteractive @PSBoundParameters;
Write-Host "Running OneShot task ``$(Get-OneShotTask)``";
switch (Get-OneShotTask) {
([OneShotTask]::InitializeMSAccount) {
Initialize-UserCreation;
}
([OneShotTask]::DisableUAC) {
Disable-UAC;
Register-Setup;
}
}
}
catch {
Set-Content -Path $errorPath -Value $Error;
Set-UserPermissions $errorPath;
}
finally {
Set-Stage ([SetupStage]::Idle);
& $taskSetter $null;
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "The OneShot task ``$(Get-OneShotTask)`` finished.";
}
exit;
}
<#

View file

@ -9,7 +9,6 @@ using namespace System.Security.Principal;
. "$PSScriptRoot/../Scripts/WSL.ps1";
. "$PSScriptRoot/../Software/Firefox/Install.ps1";
. "$PSScriptRoot/../../Common/Scripts/Context.ps1";
. "$PSScriptRoot/../../Common/Types/OneShotTask.ps1";
$null = New-Module {
. "$PSScriptRoot/../Scripts/Hooks.ps1";
@ -23,7 +22,6 @@ $null = New-Module {
. "$PSScriptRoot/../../Common/Scripts/Software.ps1";
. "$PSScriptRoot/../../Common/Scripts/SoftwareManagement.ps1";
. "$PSScriptRoot/../../Common/Types/InstallerAction.ps1";
. "$PSScriptRoot/../../Common/Types/OneShotTask.ps1";
<#
.SYNOPSIS
@ -41,29 +39,6 @@ $null = New-Module {
#>
function Start-InstallationLoop {
while (-not (Get-IsFinished)) {
switch (Get-Stage) {
($null) {
Set-Stage ([SetupStage]::Configure);
break;
}
([SetupStage]::OneShot) {
Write-Host "Running OneShot task ``$(Get-OneShotTask)``";
Start-OneShot {
switch (Get-OneShotTask) {
([OneShotTask]::InitializeMSAccount) {
Initialize-UserCreation;
}
([OneShotTask]::DisableUAC) {
Disable-UAC;
Register-Setup;
}
}
};
break;
}
default {
if (Test-Admin) {
$null = Import-Module PSWindowsUpdate;
@ -364,7 +339,11 @@ $null = New-Module {
};
}
switch ($_) {
switch (Get-Stage) {
($null) {
Set-Stage ([SetupStage]::Configure);
break;
}
([SetupStage]::Configure) {
if (Get-Config "valhalla.windows.dualboot.enable") {
if (-not (Test-Qemu)) {
@ -424,7 +403,6 @@ $null = New-Module {
logoff;
} else {
Restart-Intermediate;
exit;
}
exit;
@ -460,10 +438,10 @@ $null = New-Module {
Set-LocalUser @userArguments;
Deploy-SoftwareAction -Action ([InstallerAction]::ConfigureUser);
Remove-LocalGroupMember -Member $name @adminGroup -ErrorAction SilentlyContinue;
Remove-LocalGroupMember -Member "$name" @adminGroup -ErrorAction SilentlyContinue;
foreach ($group in Get-UserConfig -UserName $name "groups") {
Add-LocalGroupMember -Member $name -Name "$group";
foreach ($group in Get-UserConfig -UserName "$name" "groups") {
Add-LocalGroupMember -Member "$name" -Name "$group";
}
if (-not $msAccount) {
@ -479,6 +457,7 @@ $null = New-Module {
Unregister-WslDistribution;
Set-UserStage ([UserStage]::Completed);
Restart-Intermediate;
exit;
}
}
}
@ -489,8 +468,6 @@ $null = New-Module {
}
}
}
}
}
function Invoke-WindowsInstallation([Context] $context) {
$Global:InformationPreference = "Continue";