PortValhalla/scripts/Windows/Scripts/Software.ps1

66 lines
2.1 KiB
PowerShell
Raw Normal View History

2024-08-01 16:24:26 +00:00
. "$PSScriptRoot/Config.ps1";
. "$PSScriptRoot/../Types/InstallerAction.ps1";
2024-08-01 16:24:26 +00:00
2024-08-01 21:34:35 +00:00
$null = New-Module {
. "$PSScriptRoot/../Types/InstallerAction.ps1";
$userArgument = "name";
2024-08-01 16:24:26 +00:00
function Start-SoftwareInstaller {
param(
2024-08-04 22:07:41 +00:00
[string] $Name,
[scriptblock] $Installer = { },
[scriptblock] $Configurator = { },
[scriptblock] $UserConfigurator = { },
[InstallerAction] $Action = [InstallerAction]::Install,
2024-08-05 20:51:29 +00:00
[hashtable] $Arguments
2024-08-01 16:24:26 +00:00
)
2024-08-04 22:07:41 +00:00
if (-not $Name) {
$Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName));
}
2024-08-04 22:07:41 +00:00
if ($null -ne $Name) {
$Name = "``$Name``";
} else {
2024-08-04 22:07:41 +00:00
$Name = "unknown software";
}
$installHandler = {
2024-08-04 22:07:41 +00:00
param(
[InstallerAction] $Action,
[hashtable] $Arguments
)
2024-08-05 21:08:36 +00:00
$Arguments ??= @{ };
2024-08-05 20:45:17 +00:00
$argumentList = @{
installer = $installHandler;
arguments = $Arguments;
};
2024-08-04 22:07:41 +00:00
if ($action -eq ([InstallerAction]::Install)) {
Write-Host "Installing $Name";
& $Installer @argumentList;
2024-08-04 22:07:41 +00:00
} elseif ($Action -eq ([InstallerAction]::Configure)) {
Write-Host "Configuring $Name";
& $Configurator @argumentList;
2024-08-04 22:07:41 +00:00
foreach ($user in Get-Users) {
$Arguments.Add($userArgument, $user);
2024-08-05 20:49:44 +00:00
$argumentList.Add("action", [InstallerAction]::ConfigureUser);
& $installHandler @argumentList;
2024-08-04 22:07:41 +00:00
}
} elseif ($Action -eq ([InstallerAction]::ConfigureUser)) {
if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) {
$argumentList.Add($userArgument, ($env:UserName));
2024-08-04 22:07:41 +00:00
}
Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``";
& $UserConfigurator @argumentList;
2024-08-01 16:24:26 +00:00
}
};
2024-08-04 22:07:41 +00:00
& $installHandler -Action $Action -Arguments $Arguments;
2024-08-01 16:24:26 +00:00
}
}