From 36336e332dd61160fc6541becbcb5962fc815b9b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 7 Aug 2024 20:30:12 +0200 Subject: [PATCH] Add scripts for installing PowerShell --- scripts/Common/Software/PowerShell/Manage.ps1 | 60 +++++++++++++++++++ scripts/Windows/OS/Install.ps1 | 2 + scripts/Windows/Scripts/Scripting.ps1 | 46 ++++++++++++++ .../Windows/Software/PowerShell/Manage.ps1 | 21 +++++++ 4 files changed, 129 insertions(+) create mode 100644 scripts/Common/Software/PowerShell/Manage.ps1 create mode 100644 scripts/Windows/Software/PowerShell/Manage.ps1 diff --git a/scripts/Common/Software/PowerShell/Manage.ps1 b/scripts/Common/Software/PowerShell/Manage.ps1 new file mode 100644 index 00000000..7b40bf2b --- /dev/null +++ b/scripts/Common/Software/PowerShell/Manage.ps1 @@ -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; + } + } + }; diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 1839d654..5c873711 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -64,6 +64,7 @@ $null = New-Module { Invoke-Hook "Install-PSModules" -Fallback { Install-Module -AcceptLicense -Force PSWindowsUpdate; + Install-Module -AcceptLicense -Force PSScriptAnalyzer; }; if (-not (Test-Winget)) { @@ -182,6 +183,7 @@ $null = New-Module { if (Test-Collection "essential") { # Essentials . "$softwarePath/OpenSSH/Manage.ps1" @arguments; + . "$softwarePath/PowerShell/Manage.ps1"; } if (Test-Collection "common") { diff --git a/scripts/Windows/Scripts/Scripting.ps1 b/scripts/Windows/Scripts/Scripting.ps1 index e0a9f912..b9b0ebbb 100644 --- a/scripts/Windows/Scripts/Scripting.ps1 +++ b/scripts/Windows/Scripts/Scripting.ps1 @@ -14,3 +14,49 @@ function ConvertTo-Injection { [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!"; + } + } +} diff --git a/scripts/Windows/Software/PowerShell/Manage.ps1 b/scripts/Windows/Software/PowerShell/Manage.ps1 new file mode 100644 index 00000000..a52a0452 --- /dev/null +++ b/scripts/Windows/Software/PowerShell/Manage.ps1 @@ -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 }; + };