Make the install script action agnostic

This commit is contained in:
Manuel Thalmann 2024-08-28 00:03:28 +02:00
parent 8c7d22a780
commit 1e35fa150f
3 changed files with 117 additions and 47 deletions

View file

@ -2,6 +2,12 @@ using namespace Microsoft.Win32;
using namespace System.Security.AccessControl; using namespace System.Security.AccessControl;
using namespace System.Security.Principal; using namespace System.Security.Principal;
enum WindowsInstallerStage {
Initialize
Run
Completed
}
enum SetupStage { enum SetupStage {
Configure Configure
Install Install
@ -18,6 +24,7 @@ enum UserStage {
$null = New-Module { $null = New-Module {
[string] $configRoot = "HKLM:\Software\PortValhalla"; [string] $configRoot = "HKLM:\Software\PortValhalla";
[string] $stageOption = "Stage"; [string] $stageOption = "Stage";
[string] $setupStageOption = "SetupStage";
[string] $userOption = "SetupUser"; [string] $userOption = "SetupUser";
[string] $userStageOption = "UserStage"; [string] $userStageOption = "UserStage";
[string] $accountOption = "MSAccount"; [string] $accountOption = "MSAccount";
@ -272,11 +279,44 @@ $null = New-Module {
<# <#
.SYNOPSIS .SYNOPSIS
Gets the name of the current setup stage. Gets the name of the current stage of the Windows install script action.
#> #>
function Get-Stage { function Get-Stage {
$stage = Get-SetupOption $stageOption; $stage = Get-SetupOption $stageOption;
if ($null -ne $stage) {
$stage = [WindowsInstallerStage]$stage;
}
return $stage;
}
<#
.SYNOPSIS
Sets the name of the current stage of the Windows install script action.
.PARAMETER Name
The name of the stage to set.
#>
function Set-Stage {
param(
$Name
)
if (-not (($null -eq $Name) -or ($Name -is [string]))) {
$Name = ([WindowsInstallerStage]$Name).ToString();
}
$null = Set-SetupOption $stageOption $Name;
}
<#
.SYNOPSIS
Gets the name of the current setup stage.
#>
function Get-SetupStage {
$stage = Get-SetupOption $setupStageOption;
if ($null -ne $stage) { if ($null -ne $stage) {
$stage = [SetupStage]$stage; $stage = [SetupStage]$stage;
} }
@ -291,7 +331,7 @@ $null = New-Module {
.PARAMETER Name .PARAMETER Name
The name to set the current stage to. The name to set the current stage to.
#> #>
function Set-Stage { function Set-SetupStage {
param( param(
$Name $Name
) )
@ -300,7 +340,7 @@ $null = New-Module {
$Name = ([SetupStage]$Name).ToString(); $Name = ([SetupStage]$Name).ToString();
} }
$null = Set-SetupOption $stageOption $Name; $null = Set-SetupOption $setupStageOption $Name;
} }
<# <#

View file

@ -17,6 +17,7 @@ $null = New-Module {
. "$PSScriptRoot/../Scripts/Security.ps1"; . "$PSScriptRoot/../Scripts/Security.ps1";
. "$PSScriptRoot/../Scripts/Update.ps1"; . "$PSScriptRoot/../Scripts/Update.ps1";
. "$PSScriptRoot/../Scripts/Users.ps1"; . "$PSScriptRoot/../Scripts/Users.ps1";
. "$PSScriptRoot/../Types/WindowsInstallerAction.ps1";
. "$PSScriptRoot/../../Common/Scripts/Config.ps1"; . "$PSScriptRoot/../../Common/Scripts/Config.ps1";
. "$PSScriptRoot/../../Common/Scripts/Operations.ps1"; . "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
. "$PSScriptRoot/../../Common/Scripts/Software.ps1"; . "$PSScriptRoot/../../Common/Scripts/Software.ps1";
@ -29,7 +30,7 @@ $null = New-Module {
#> #>
function Start-WindowsInstallation { function Start-WindowsInstallation {
Start-Operation -NoImplicitCleanup { Start-Operation -NoImplicitCleanup {
Start-InstallationLoop; Start-InstallationLoop ([WindowsInstallerAction]::Install);
}; };
} }
@ -38,6 +39,23 @@ $null = New-Module {
Starts the installation loop. Starts the installation loop.
#> #>
function Start-InstallationLoop { function Start-InstallationLoop {
param(
[WindowsInstallerAction] $Action
)
while ((Get-Stage) -ne ([WindowsInstallerStage]::Completed)) {
switch (Get-Stage) {
($null) {
Set-Stage ([WindowsInstallerStage]::Initialize);
break;
}
([WindowsInstallerStage]::Initialize) {
Set-Stage ([WindowsInstallerStage]::Run);
break;
}
([WindowsInstallerStage]::Run) {
switch ($Action) {
([WindowsInstallerAction]::Install) {
while (-not (Get-IsFinished)) { while (-not (Get-IsFinished)) {
if (Test-Admin) { if (Test-Admin) {
$null = Import-Module PSWindowsUpdate; $null = Import-Module PSWindowsUpdate;
@ -52,9 +70,9 @@ $null = New-Module {
} }
} }
switch (Get-Stage) { switch (Get-SetupStage) {
($null) { ($null) {
Set-Stage ([SetupStage]::Configure); Set-SetupStage ([SetupStage]::Configure);
break; break;
} }
([SetupStage]::Configure) { ([SetupStage]::Configure) {
@ -82,12 +100,12 @@ $null = New-Module {
} }
} }
Set-Stage ([SetupStage]::Install); Set-SetupStage ([SetupStage]::Install);
} }
([SetupStage]::Install) { ([SetupStage]::Install) {
Write-Host "Entering install phase"; Write-Host "Entering install phase";
Deploy-SoftwareAction; Deploy-SoftwareAction;
Set-Stage ([SetupStage]::CreateUser); Set-SetupStage ([SetupStage]::CreateUser);
} }
([SetupStage]::CreateUser) { ([SetupStage]::CreateUser) {
Install-ValhallaUsers; Install-ValhallaUsers;
@ -96,6 +114,14 @@ $null = New-Module {
} }
} }
} }
}
Set-Stage ([WindowsInstallerStage]::Completed);
break;
}
}
}
}
function Invoke-WindowsInstallation([Context] $context) { function Invoke-WindowsInstallation([Context] $context) {
$Global:InformationPreference = "Continue"; $Global:InformationPreference = "Continue";

View file

@ -0,0 +1,4 @@
enum WindowsInstallerAction {
Backup
Install
}