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; using namespace System.Security.Principal;
enum SetupStage { enum SetupStage {
Idle
OneShot
Configure Configure
Install Install
CreateUser CreateUser
@ -389,7 +387,7 @@ $null = New-Module {
Gets a value indicating whether the setup has finished. Gets a value indicating whether the setup has finished.
#> #>
function Get-IsFinished { 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 { function Start-Operation {
param( param(
[switch] $NonInteractive = ((Get-Stage) -eq ([SetupStage]::OneShot)), [switch] $NonInteractive,
[switch] $NoImplicitCleanup, [switch] $NoImplicitCleanup,
[scriptblock] $Action [scriptblock] $Action
) )
$cleanup = { }; $cleanup = { };
$taskPending = $false;
if (-not $Global:InOperation) { if (-not $Global:InOperation) {
if ($env:DEBUG) { if ($env:DEBUG) {
Set-PSDebug -Trace 1; Set-PSDebug -Trace 1;
} }
if ($null -ne (Get-OneShotTask)) {
$taskPending = $true;
[switch] $NonInteractive = $true;
}
$Global:InOperation = $true; $Global:InOperation = $true;
$Global:ErrorActionPreference = $NonInteractive.IsPresent ? 'Continue' : 'Inquire'; $Global:ErrorActionPreference = $NonInteractive.IsPresent ? 'Continue' : 'Inquire';
@ -229,7 +235,12 @@ $null = New-Module {
} }
} }
if ($taskPending) {
Start-OneShot;
} else {
& $Action; & $Action;
}
& $cleanup; & $cleanup;
} }
@ -238,7 +249,13 @@ $null = New-Module {
Gets the current OneShot task. Gets the current OneShot task.
#> #>
function Get-OneShotTask { 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 [OneShotTask] $Task
) )
$currentStage = Get-Stage;
Set-Stage ([SetupStage]::OneShot);
& $taskSetter $Task; & $taskSetter $Task;
& { & {
@ -321,8 +336,6 @@ $null = New-Module {
} }
}; };
Set-Stage $currentStage;
if (Test-Path $errorPath) { if (Test-Path $errorPath) {
$errorMessage = Get-Content $errorPath; $errorMessage = Get-Content $errorPath;
Remove-Item $errorPath; Remove-Item $errorPath;
@ -340,23 +353,27 @@ $null = New-Module {
Executes the specified action and notifies the OneShot task executor. Executes the specified action and notifies the OneShot task executor.
#> #>
function Start-OneShot { function Start-OneShot {
param(
[scriptblock] $Action
)
try { 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 { catch {
Set-Content -Path $errorPath -Value $Error; Set-Content -Path $errorPath -Value $Error;
Set-UserPermissions $errorPath; Set-UserPermissions $errorPath;
} }
finally { finally {
Set-Stage ([SetupStage]::Idle); & $taskSetter $null;
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "The OneShot task ``$(Get-OneShotTask)`` finished."; 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/../Scripts/WSL.ps1";
. "$PSScriptRoot/../Software/Firefox/Install.ps1"; . "$PSScriptRoot/../Software/Firefox/Install.ps1";
. "$PSScriptRoot/../../Common/Scripts/Context.ps1"; . "$PSScriptRoot/../../Common/Scripts/Context.ps1";
. "$PSScriptRoot/../../Common/Types/OneShotTask.ps1";
$null = New-Module { $null = New-Module {
. "$PSScriptRoot/../Scripts/Hooks.ps1"; . "$PSScriptRoot/../Scripts/Hooks.ps1";
@ -23,7 +22,6 @@ $null = New-Module {
. "$PSScriptRoot/../../Common/Scripts/Software.ps1"; . "$PSScriptRoot/../../Common/Scripts/Software.ps1";
. "$PSScriptRoot/../../Common/Scripts/SoftwareManagement.ps1"; . "$PSScriptRoot/../../Common/Scripts/SoftwareManagement.ps1";
. "$PSScriptRoot/../../Common/Types/InstallerAction.ps1"; . "$PSScriptRoot/../../Common/Types/InstallerAction.ps1";
. "$PSScriptRoot/../../Common/Types/OneShotTask.ps1";
<# <#
.SYNOPSIS .SYNOPSIS
@ -41,29 +39,6 @@ $null = New-Module {
#> #>
function Start-InstallationLoop { function Start-InstallationLoop {
while (-not (Get-IsFinished)) { 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) { if (Test-Admin) {
$null = Import-Module PSWindowsUpdate; $null = Import-Module PSWindowsUpdate;
@ -364,7 +339,11 @@ $null = New-Module {
}; };
} }
switch ($_) { switch (Get-Stage) {
($null) {
Set-Stage ([SetupStage]::Configure);
break;
}
([SetupStage]::Configure) { ([SetupStage]::Configure) {
if (Get-Config "valhalla.windows.dualboot.enable") { if (Get-Config "valhalla.windows.dualboot.enable") {
if (-not (Test-Qemu)) { if (-not (Test-Qemu)) {
@ -424,7 +403,6 @@ $null = New-Module {
logoff; logoff;
} else { } else {
Restart-Intermediate; Restart-Intermediate;
exit;
} }
exit; exit;
@ -460,10 +438,10 @@ $null = New-Module {
Set-LocalUser @userArguments; Set-LocalUser @userArguments;
Deploy-SoftwareAction -Action ([InstallerAction]::ConfigureUser); 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") { foreach ($group in Get-UserConfig -UserName "$name" "groups") {
Add-LocalGroupMember -Member $name -Name "$group"; Add-LocalGroupMember -Member "$name" -Name "$group";
} }
if (-not $msAccount) { if (-not $msAccount) {
@ -479,6 +457,7 @@ $null = New-Module {
Unregister-WslDistribution; Unregister-WslDistribution;
Set-UserStage ([UserStage]::Completed); Set-UserStage ([UserStage]::Completed);
Restart-Intermediate; Restart-Intermediate;
exit;
} }
} }
} }
@ -489,8 +468,6 @@ $null = New-Module {
} }
} }
} }
}
}
function Invoke-WindowsInstallation([Context] $context) { function Invoke-WindowsInstallation([Context] $context) {
$Global:InformationPreference = "Continue"; $Global:InformationPreference = "Continue";