Add scripts for editing the powershell profile

This commit is contained in:
Manuel Thalmann 2024-03-20 16:33:18 +01:00
parent 55b94b0ec4
commit d767f0e97d
2 changed files with 60 additions and 24 deletions

View file

@ -0,0 +1,46 @@
#!/bin/pwsh
$null = New-Module {
function Add-PowerShellProfileStatement() {
param (
[string]$HomeDir = "~",
[string]$Category = $null,
[Parameter(Position=0, Mandatory=$true)]
[string]$Statement,
[bool]$Overwrite = $false
)
[System.Collections.Generic.List[string]] $profiles = @();
if (Get-Command pwsh) {
$profiles.Add($(pwsh -c '$PROFILE'));
}
if (Get-Command powershell) {
$profiles.Add($(powershell -c '$PROFILE'));
}
Push-Location ~;
$profiles = $profiles | ForEach-Object { Resolve-Path -Relative $_ };
if ($Category) {
$profiles = $profiles | ForEach-Object { Join-Path (Split-Path -Parent $_) "profile.d" "$Category.ps1"; };
}
$profiles | ForEach-Object {
$fileName = "$HomeDir/$_";
$dirName = Split-Path -Parent $fileName;
if (-not (Test-Path -PathType Container $dirName)) {
New-Item -ItemType Directory $dirName;
}
if ((Test-Path -PathType Leaf $fileName) -and (-not $Overwrite)) {
Add-Content -Force "$fileName" "`n$Statement";
} else {
Set-Content -Force "$fileName" "$Statement";
}
};
Pop-Location;
}
}

View file

@ -1,4 +1,6 @@
#!/bin/pwsh #!/bin/pwsh
. "$PSScriptRoot/../../Common/Config/powershell/profile.ps1";
class Context { class Context {
[string]$EntryPoint; [string]$EntryPoint;
[string]$RootDir; [string]$RootDir;
@ -47,41 +49,29 @@ class Context {
} }
[void] AddPowerShellProfileStatement([bool] $defaultUser, [string] $category, [string] $statement) { [void] AddPowerShellProfileStatement([bool] $defaultUser, [string] $category, [string] $statement) {
[string] $userDir = $null; [string] $homeDir = $null;
if ($defaultUser) { if ($defaultUser) {
$userDir = "C:\Users\Default"; $homeDir = "C:\Users\Default";
} else { } else {
$userDir = "~"; $homeDir = "~";
} }
$this.AddPowerShellProfileStatement($userDir, $category, $statement); $this.AddPowerShellProfileStatement($homeDir, $category, $statement);
} }
[void] AddPowerShellProfileStatement([string] $userDir, [string] $category, [string] $statement) { [void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement) {
Push-Location ~;
$profileFiles = @((powershell -c '$PROFILE'), (pwsh -c '$PROFILE')) | ForEach-Object { Resolve-Path -Relative $_ };
if ($category) { if ($category) {
$profileFiles = $profileFiles | ForEach-Object { Join-Path (Split-Path -Parent $_) "profile.d" "$category.ps1"; }; $overwrite = $true;
} else {
$overwrite = $false;
} }
$profileFiles | ForEach-Object { $this.AddPowerShellProfileStatement($homeDir, $category, $statement, $overwrite);
$fileName = "$userDir\$_"; }
$dirName = Split-Path -Parent $fileName;
if (-not (Test-Path -PathType Container $dirName)) { [void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement, [bool] $overwrite) {
New-Item -ItemType Directory $dirName; Add-PowerShellProfileStatement -HomeDir $homeDir -Category $category -Statement $statement -Overwrite $overwrite;
}
if (Test-Path -PathType Leaf $fileName) {
Add-Content -Force "$fileName" "`n$statement";
} else {
Set-Content -Force "$fileName" "$statement";
}
};
Pop-Location;
} }
[Microsoft.Win32.RegistryKey] EnsureConfigKey() { [Microsoft.Win32.RegistryKey] EnsureConfigKey() {