Allow global installation of powershell configs

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

View file

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

View file

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