Allow global installation of powershell configs

This commit is contained in:
Manuel Thalmann 2024-03-20 23:49:21 +01:00
parent 5cdc395cef
commit b07f4ce23b
2 changed files with 41 additions and 28 deletions

View file

@ -2,16 +2,30 @@
$null = New-Module { $null = New-Module {
function Add-PowerShellProfileStatement() { function Add-PowerShellProfileStatement() {
param ( param (
[Parameter(ParameterSetName="Global", Mandatory)]
[switch]$System,
[Parameter(ParameterSetName="Home")]
[string]$HomeDir = "~", [string]$HomeDir = "~",
[Parameter(ParameterSetName="Profile", Mandatory)]
[string]$ProfileRoot,
[Parameter(ParameterSetName="Profile", Mandatory)]
[Parameter(ParameterSetName="Global", Mandatory)]
[Parameter(ParameterSetName="Home")]
[string]$Category = $null, [string]$Category = $null,
[Parameter(Position=0, Mandatory=$true)] [Parameter(Position=0, Mandatory=$true)]
[string]$Statement, [string]$Statement,
[switch]$Overwrite [switch]$Overwrite
) )
[System.Collections.Generic.List[string]] $shells = @();
[System.Collections.Generic.List[string]] $profiles = @(); [System.Collections.Generic.List[string]] $profiles = @();
if ($System) {
$ProfileRoot = "$env:ProgramData/powershell";
}
if (-not $ProfileRoot) {
[System.Collections.Generic.List[string]] $shells = @();
if (Get-Command pwsh -ErrorAction SilentlyContinue) { if (Get-Command pwsh -ErrorAction SilentlyContinue) {
$shells.Add("pwsh"); $shells.Add("pwsh");
} }
@ -27,6 +41,9 @@ $null = New-Module {
Push-Location ~; Push-Location ~;
$profiles = $profiles | ForEach-Object { [System.IO.Path]::GetRelativePath($(Get-Location), $_); }; $profiles = $profiles | ForEach-Object { [System.IO.Path]::GetRelativePath($(Get-Location), $_); };
} else {
$profiles = @("$ProfileRoot/.");
}
if ($Category) { if ($Category) {
if (-not $($Overwrite.IsPresent)) { if (-not $($Overwrite.IsPresent)) {

View file

@ -44,34 +44,30 @@ class Context {
$this.AddPowerShellProfileStatement($true, $category, $statement); $this.AddPowerShellProfileStatement($true, $category, $statement);
} }
[void] AddPowerShellProfileStatement([bool] $defaultUser, [string] $statement) { [void] AddPowerShellProfileStatement([bool] $system, [string] $statement) {
$this.AddPowerShellProfileStatement($defaultUser, $null, $statement); $this.AddPowerShellProfileStatement($system, $null, $statement);
} }
[void] AddPowerShellProfileStatement([bool] $defaultUser, [string] $category, [string] $statement) { [void] AddPowerShellProfileStatement([bool] $system, [string] $category, [string] $statement) {
[string] $homeDir = $null; $this.AddPowerShellProfileStatement($system, $category, $statement);
if ($defaultUser) {
$homeDir = "C:\Users\Default";
} else {
$homeDir = "~";
} }
$this.AddPowerShellProfileStatement($homeDir, $category, $statement); [void] AddPowerShellProfileStatement([bool] $system, [string] $category, [string] $statement) {
}
[void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement) {
if ($category) { if ($category) {
$overwrite = $true; $overwrite = $true;
} else { } else {
$overwrite = $false; $overwrite = $false;
} }
$this.AddPowerShellProfileStatement($homeDir, $category, $statement, $overwrite); $this.AddPowerShellProfileStatement($system, $category, $statement, $overwrite);
} }
[void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement, [bool] $overwrite) { [void] AddPowerShellProfileStatement([bool] $system, [string] $category, [string] $statement, [bool] $overwrite) {
Add-PowerShellProfileStatement -HomeDir $homeDir -Category $category -Statement $statement -Overwrite $overwrite; if ($system) {
Add-PowerShellProfileStatement -System -Category $category -Statement $statement -Overwrite $overwrite;
} else {
Add-PowerShellProfileStatement -Category $category -Statement $statement -Overwrite $overwrite;
}
} }
[Microsoft.Win32.RegistryKey] EnsureConfigKey() { [Microsoft.Win32.RegistryKey] EnsureConfigKey() {