2024-07-31 13:42:02 +00:00
|
|
|
using namespace Microsoft.Win32;
|
|
|
|
using namespace System.Security.AccessControl;
|
|
|
|
using namespace System.Security.Principal;
|
|
|
|
|
2024-07-31 14:25:32 +00:00
|
|
|
enum SetupStage {
|
2024-07-31 14:52:49 +00:00
|
|
|
Initialize
|
2024-08-01 14:32:03 +00:00
|
|
|
Configure
|
2024-07-31 14:52:49 +00:00
|
|
|
Install
|
2024-08-08 02:38:40 +00:00
|
|
|
CreateUser
|
2024-07-31 14:25:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 13:42:02 +00:00
|
|
|
$null = New-Module {
|
|
|
|
[string] $configRoot = "HKLM:\Software\PortValhalla";
|
|
|
|
[string] $stageOption = "Stage";
|
2024-07-31 14:09:50 +00:00
|
|
|
[string] $finishedOption = "Finished";
|
2024-07-31 13:42:02 +00:00
|
|
|
[RegistryKey] $key = $null;
|
|
|
|
|
2024-08-01 14:12:58 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Converts the specified path to linux and escapes it for the use in a script.
|
|
|
|
|
|
|
|
.PARAMETER Path
|
|
|
|
The path to convert.
|
|
|
|
#>
|
|
|
|
function ConvertTo-LinuxPath {
|
|
|
|
param(
|
|
|
|
[string] $Path
|
|
|
|
)
|
|
|
|
|
|
|
|
$job = Start-Job {
|
|
|
|
$env:Value = Resolve-Path $Using:Path;
|
|
|
|
$env:WSLENV = "Value/p";
|
|
|
|
$result = wsl -- bash -c 'echo "$Value"';
|
|
|
|
wsl -e printf "%q" "$result";
|
|
|
|
};
|
|
|
|
|
|
|
|
Receive-Job -Wait $job;
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:42:02 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the registry key containing options related to the setup.
|
|
|
|
#>
|
2024-07-31 13:52:23 +00:00
|
|
|
function Get-SetupConfigKey {
|
2024-07-31 13:42:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:12:58 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-08-01 15:56:42 +00:00
|
|
|
Runs a script based on the `config.fish` script.
|
2024-08-01 14:12:58 +00:00
|
|
|
|
2024-08-01 15:56:42 +00:00
|
|
|
.PARAMETER Script
|
|
|
|
The script to run.
|
2024-08-01 14:12:58 +00:00
|
|
|
#>
|
2024-08-01 15:56:42 +00:00
|
|
|
function Invoke-ConfigScript {
|
2024-08-01 14:12:58 +00:00
|
|
|
param(
|
2024-08-01 15:56:42 +00:00
|
|
|
[string] $Script
|
2024-08-01 14:12:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
$scriptPath = "$PSScriptRoot/../../Common/Scripts/config.fish";
|
|
|
|
|
2024-08-08 16:21:16 +00:00
|
|
|
if (-not $IsWindows) {
|
|
|
|
$escapedPath = (fish -c 'string escape $argv' "$scriptPath");
|
|
|
|
fish -c ". $escapedPath; $Script";
|
|
|
|
} else {
|
|
|
|
function fish {
|
|
|
|
wsl --shell-type login -- nix --extra-experimental-features "nix-command flakes" run nixpkgs`#fish -- $args
|
|
|
|
}
|
2024-08-01 14:12:58 +00:00
|
|
|
|
2024-08-08 16:21:16 +00:00
|
|
|
fish -c ". $(ConvertTo-LinuxPath $scriptPath); $Script" | ConvertFrom-Json;
|
|
|
|
}
|
2024-08-01 15:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets a configuration option.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
The name of the option to get.
|
|
|
|
#>
|
|
|
|
function Get-Config {
|
2024-08-08 12:45:34 +00:00
|
|
|
param(
|
|
|
|
[string] $Name,
|
|
|
|
[Parameter(ValueFromRemainingArguments)]
|
|
|
|
[string[]] $ArgumentList
|
|
|
|
)
|
|
|
|
|
|
|
|
Invoke-ConfigScript "getConfig $Name --json $ArgumentList";
|
|
|
|
}
|
|
|
|
|
2024-08-08 15:49:43 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets a user configuration.
|
|
|
|
|
|
|
|
.PARAMETER UserName
|
|
|
|
The name of the user to get the configuration for.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
The name of the configuration to get.
|
|
|
|
#>
|
|
|
|
function Get-UserConfig {
|
|
|
|
param(
|
|
|
|
[string] $UserName = $env:UserName,
|
|
|
|
[Parameter(Mandatory, Position = 0)]
|
|
|
|
[string] $Name
|
|
|
|
)
|
|
|
|
|
|
|
|
if ((Get-Users) -contains $UserName) {
|
|
|
|
Get-Config "valhalla.windows.users.$UserName.$Name";
|
|
|
|
} else {
|
|
|
|
return $null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 12:45:34 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the attributes of a configuration object.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
The name of the configuration to get the attributes of.
|
|
|
|
#>
|
|
|
|
function Get-Attributes {
|
2024-08-01 15:56:42 +00:00
|
|
|
param(
|
|
|
|
[string] $Name
|
|
|
|
)
|
|
|
|
|
2024-08-08 12:45:34 +00:00
|
|
|
Invoke-ConfigScript "getAttributes $Name";
|
2024-08-01 15:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the names of the users to create.
|
|
|
|
#>
|
|
|
|
function Get-Users {
|
2024-08-09 01:02:17 +00:00
|
|
|
[OutputType([string[]])]
|
|
|
|
param()
|
2024-08-08 12:45:34 +00:00
|
|
|
Get-Attributes "valhalla.windows.users";
|
2024-08-01 14:12:58 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 13:42:02 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the value of an option related to the setup.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
The name of the option value to get.
|
|
|
|
#>
|
2024-07-31 13:52:23 +00:00
|
|
|
function Get-SetupOption {
|
2024-07-31 13:42:02 +00:00
|
|
|
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.
|
|
|
|
#>
|
2024-07-31 13:52:23 +00:00
|
|
|
function Set-SetupOption {
|
2024-07-31 13:42:02 +00:00
|
|
|
param(
|
|
|
|
[string] $Name,
|
|
|
|
$Value
|
|
|
|
)
|
|
|
|
|
|
|
|
$key = Get-SetupConfigKey;
|
2024-07-31 20:33:16 +00:00
|
|
|
$null = Set-ItemProperty ($key.PSPath) -Name $Name -Value $Value;
|
2024-07-31 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the name of the current setup stage.
|
|
|
|
#>
|
2024-07-31 13:52:23 +00:00
|
|
|
function Get-Stage {
|
2024-07-31 20:33:37 +00:00
|
|
|
$stage = Get-SetupOption $stageOption;
|
|
|
|
|
|
|
|
if ($null -ne $stage) {
|
|
|
|
$stage = [SetupStage]$stage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stage;
|
2024-07-31 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Sets the current stage.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
The name to set the current stage to.
|
|
|
|
#>
|
2024-07-31 13:52:23 +00:00
|
|
|
function Set-Stage {
|
2024-07-31 13:42:02 +00:00
|
|
|
param(
|
2024-07-31 14:52:49 +00:00
|
|
|
$Name
|
2024-07-31 13:42:02 +00:00
|
|
|
)
|
|
|
|
|
2024-07-31 20:33:37 +00:00
|
|
|
if (-not (($null -eq $Name) -or ($Name -is [string]))) {
|
2024-07-31 14:52:49 +00:00
|
|
|
$Name = ([SetupStage]$Name).ToString();
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:42:02 +00:00
|
|
|
$null = Set-SetupOption $stageOption $Name;
|
|
|
|
}
|
2024-07-31 14:09:50 +00:00
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets a value indicating whether the setup has finished.
|
|
|
|
#>
|
|
|
|
function Get-IsFinished {
|
|
|
|
return [bool] (Get-SetupOption $finishedOption);
|
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Sets a value indicating whether the setup has finished.
|
|
|
|
#>
|
|
|
|
function Set-IsFinished {
|
|
|
|
param(
|
|
|
|
$Value
|
|
|
|
)
|
2024-08-01 14:32:03 +00:00
|
|
|
|
|
|
|
Set-SetupOption $finishedOption $true;
|
2024-07-31 14:09:50 +00:00
|
|
|
}
|
2024-07-31 14:52:49 +00:00
|
|
|
|
2024-08-05 22:04:02 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Checks whether the specified software collection is enabled.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
The name of the collection to check.
|
|
|
|
#>
|
|
|
|
function Test-Collection {
|
|
|
|
param(
|
|
|
|
[string] $Name
|
|
|
|
)
|
|
|
|
|
|
|
|
Get-Config "valhalla.software.$Name";
|
|
|
|
}
|
|
|
|
|
2024-08-06 12:32:37 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Checks whether the current user is the setup user.
|
|
|
|
#>
|
|
|
|
function Test-SetupUser {
|
|
|
|
$env:UserName -eq (Get-Config "valhalla.windows.setupUser");
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:52:49 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Checks whether the active session is executed with admin rights.
|
|
|
|
#>
|
|
|
|
function Test-Admin {
|
|
|
|
net session 2> $null | Out-Null;
|
|
|
|
return $?;
|
|
|
|
}
|
2024-07-31 13:42:02 +00:00
|
|
|
}
|