Add scripts for installing PowerShell

This commit is contained in:
Manuel Thalmann 2024-08-07 20:30:12 +02:00
parent 77afa05acd
commit 82a28e1323
4 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1,60 @@
param (
$Action,
[hashtable] $Arguments
)
. "$PSScriptRoot/../../../Windows/Scripts/Software.ps1";
. "$PSScriptRoot/../../../Windows/Scripts/Scripting.ps1";
. "$PSScriptRoot/../../../Windows/Types/InstallerAction.ps1";
Start-SoftwareInstaller @PSBoundParameters `
-Installer {
param(
[scriptblock] $Installer
)
& $Installer -Action ([InstallerAction]::Configure);
} `
-Configurator {
param(
[hashtable] $Arguments
)
[string] $globalDir = $null;
$indicator = "# Profile Files";
if ($Arguments.Linux) {
$globalDir = '"/etc/powershell/conf.d"';
} else {
$globalDir = '"$env:ProgramData/PowerShell/conf.d"';
}
[System.Collections.ArrayList] $files = @($PROFILE);
if (Test-Command powershell) {
$null = $files.Add((powershell -c '$PROFILE'));
}
foreach ($file in $files) {
if (-not ((Test-Path -PathType Leaf $file) -and ((Get-Content $file) -contains $indicator))) {
Write-PSScript -FileName $file -Script (@(
$indicator,
"`$globalDir = $globalDir",
({
$profileRoot = Split-Path -Parent $PROFILE;
$profilePaths = @(
"$profileRoot/conf.d/*.ps1",
"$globalDir/*.ps1"
)
foreach ($profilePath in $profilePaths) {
if (Test-Path $profilePath) {
Get-Item $profilePath | ForEach-Object { . $_; };
}
}
}).ToString()) -join "`n") `
-Append;
}
}
};

View file

@ -63,6 +63,7 @@ $null = New-Module {
Invoke-Hook "Install-PSModules" -Fallback { Invoke-Hook "Install-PSModules" -Fallback {
Install-Module -AcceptLicense -Force PSWindowsUpdate; Install-Module -AcceptLicense -Force PSWindowsUpdate;
Install-Module -AcceptLicense -Force PSScriptAnalyzer;
}; };
if (-not (Test-Winget)) { if (-not (Test-Winget)) {
@ -181,6 +182,7 @@ $null = New-Module {
if (Test-Collection "essential") { if (Test-Collection "essential") {
# Essentials # Essentials
. "$softwarePath/OpenSSH/Manage.ps1" @arguments; . "$softwarePath/OpenSSH/Manage.ps1" @arguments;
. "$softwarePath/PowerShell/Manage.ps1";
} }
if (Test-Collection "common") { if (Test-Collection "common") {

View file

@ -14,3 +14,49 @@ function ConvertTo-Injection {
[System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($value)) [System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($value))
)')))" )')))"
} }
<#
.SYNOPSIS
Writes a PowerShell script to a specified location.
.PARAMETER FileName
The name of the file to write the script to.
.PARAMETER Script
The script to write to the file.
.PARAMETER Replace
A value indicating whether the file should be overwritten if it exists.
.PARAMETER Append
A value indicating whether the content should be appended if the file already exists.
#>
function Write-PSScript {
param(
[string] $FileName,
[string] $Script,
[Parameter(ParameterSetName="Replace")]
[switch] $Replace,
[Parameter(ParameterSetName="Append")]
[switch] $Append
)
Import-Module PSScriptAnalyzer;
$dirName = Split-Path -Parent $FileName;
$content = Invoke-Formatter -ScriptDefinition $Script;
$exists = Test-Path -PathType Leaf $FileName;
if (-not (Test-Path -PathType Container $dirName)) {
$null = New-Item -ItemType Directory $dirName;
}
if ($exists -and ($Append.IsPresent)) {
Add-Content -Force $FileName "`n$content";
} else {
if ((-not $exists) -or $Replace.IsPresent) {
Set-Content -Force $FileName $content;
} else {
Write-Host "The file ``$FileName`` already exists!";
}
}
}

View file

@ -0,0 +1,21 @@
param (
$Action,
[hashtable] $Arguments
)
. "$PSScriptRoot/../../Scripts/Software.ps1";
. "$PSScriptRoot/../../Types/InstallerAction.ps1";
Start-SoftwareInstaller @PSBoundParameters `
-Installer {
param(
[scriptblock] $Installer
)
& $Installer -Action ([InstallerAction]::Configure);
} `
-Configurator {
. "$PSScriptRoot/../../../Common/Software/PowerShell/Manage.ps1" `
-Action ([InstallerAction]::Configure) `
-Arguments @{ Linux = $false };
};