From 6ff93d2060d0584f597d0723a7228ef30e351db6 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 31 Jul 2024 15:42:02 +0200 Subject: [PATCH] Add a script for managing setup options --- scripts/Windows/Scripts/SetupConfig.ps1 | 102 ++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 scripts/Windows/Scripts/SetupConfig.ps1 diff --git a/scripts/Windows/Scripts/SetupConfig.ps1 b/scripts/Windows/Scripts/SetupConfig.ps1 new file mode 100644 index 00000000..69c39020 --- /dev/null +++ b/scripts/Windows/Scripts/SetupConfig.ps1 @@ -0,0 +1,102 @@ +using namespace Microsoft.Win32; +using namespace System.Security.AccessControl; +using namespace System.Security.Principal; + +$null = New-Module { + [string] $configRoot = "HKLM:\Software\PortValhalla"; + [string] $stageOption = "Stage"; + [RegistryKey] $key = $null; + + <# + .SYNOPSIS + Gets the registry key containing options related to the setup. + #> + function Get-SetupConfigKey() { + if (-not (Test-Path $configRoot)) { + $key = New-Item $configRoot; + $acl = Get-Acl $configRoot; + + $acl.AddAccessRule( + [RegistryAccessRule]::new( + [SecurityIdentifier]::new([WellKnownSidType]::BuiltinUsersSid, $null), + [RegistryRights]::FullControl, + [InheritanceFlags]::ObjectInherit -bor [InheritanceFlags]::ContainerInherit, + [PropagationFlags]::None, + [AccessControlType]::Allow)); + + Set-Acl $configRoot $acl; + } else { + $key = Get-Item $configRoot; + } + + return $key; + } + + <# + .SYNOPSIS + Gets the value of an option related to the setup. + + .PARAMETER Name + The name of the option value to get. + #> + function Get-SetupOption() { + param( + [string] $Name + ) + + $key = Get-SetupConfigKey; + + if ($key.GetValueNames().Contains($Name)) { + return $key.GetValue($Name); + } else { + return $null; + } + } + + <# + .SYNOPSIS + Sets the value of an option related to the setup. + + .PARAMETER Name + The name of the option to set. + + .PARAMETER Value + The value to set the option to. + #> + function Set-SetupOption() { + param( + [string] $Name, + $Value + ) + + if ($Value -is [string]) { + + } + + $key = Get-SetupConfigKey; + return $key.SetValue($Value); + } + + <# + .SYNOPSIS + Gets the name of the current setup stage. + #> + function Get-Stage() { + return Get-SetupOption $stageOption; + } + + <# + .SYNOPSIS + Sets the current stage. + + .PARAMETER Name + The name to set the current stage to. + #> + function Set-Stage() { + param( + [string] $Name + ) + + $null = Set-SetupOption $stageOption $Name; + } +}