PortValhalla/scripts/Common/Software/PowerShell/Module.ps1

73 lines
2.1 KiB
PowerShell
Raw Normal View History

2024-08-07 20:55:25 +00:00
. "$PSScriptRoot/../../Software/PowerShell/Profile.ps1";
. "$PSScriptRoot/../../Types/InstallerAction.ps1";
$null = New-Module {
<#
.SYNOPSIS
Generates the components for creating a module installer.
.PARAMETER Name
The name of the module to install.
.PARAMETER NativeOnly
A value indicating whether the module is installed in Windows PowerShell only.
.PARAMETER NoProfile
A value indicating whether the module is not added to the profile script of users.
2024-08-07 20:55:25 +00:00
#>
function Get-ModuleInstallerComponents {
param(
2024-08-08 00:28:23 +00:00
[string] $Name,
[switch] $NativeOnly,
[switch] $NoProfile
2024-08-07 20:55:25 +00:00
)
@{
arguments = @{
name = $Name;
2024-08-08 00:28:23 +00:00
nativeOnly = $NativeOnly;
2024-08-07 20:55:25 +00:00
};
installer = {
param(
[scriptblock] $Installer,
[hashtable] $Arguments
)
$env:PENDING_MODULE_NAME = $Arguments.Name;
$installAction = {
$module = $env:PENDING_MODULE_NAME;
if (-not (Get-Module -ListAvailable $module -ErrorAction SilentlyContinue)) {
Install-Module -Scope AllUsers -Force $module @args;
}
};
2024-08-08 00:28:23 +00:00
if (-not $Arguments.NativeOnly) {
& $installAction -AcceptLicense;
}
2024-08-07 20:55:25 +00:00
if (Test-Command powershell) {
powershell -c ($installAction.ToString());
}
Remove-Item Env:\PENDING_MODULE_NAME;
& $Installer -Action ([InstallerAction]::Configure) @PSBoundParameters;
};
configurator = ($NoProfile.IsPresent) ? { } : {
2024-08-07 20:55:25 +00:00
param(
[hashtable] $Arguments
)
$name = $Arguments.Name;
2024-08-07 20:55:25 +00:00
Add-PowerShellProfileStatement `
-DefaultUser `
-Category $name `
-Script "Import-Module `"$name`";";
2024-08-07 20:55:25 +00:00
};
}
}
};