Execute OneShot tasks in any operation

This commit is contained in:
Manuel Thalmann 2024-08-27 14:05:16 +02:00
parent 1ed831c4c7
commit 6615665bac
3 changed files with 432 additions and 438 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

@ -38,29 +38,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;
@ -361,7 +338,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)) {
@ -482,8 +463,6 @@ $null = New-Module {
} }
} }
} }
}
}
function Invoke-WindowsInstallation([Context] $context) { function Invoke-WindowsInstallation([Context] $context) {
$Global:InformationPreference = "Continue"; $Global:InformationPreference = "Continue";