From 5ea7b24495b0ccd54644fbf83fb145f626a4d8f3 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Sat, 31 Jan 2026 18:47:24 -0500 Subject: [PATCH] Add src/archives/samy.functions.ps1 --- src/archives/samy.functions.ps1 | 393 ++++++++++++++++++++++++++++++++ 1 file changed, 393 insertions(+) create mode 100644 src/archives/samy.functions.ps1 diff --git a/src/archives/samy.functions.ps1 b/src/archives/samy.functions.ps1 new file mode 100644 index 0000000..42d3926 --- /dev/null +++ b/src/archives/samy.functions.ps1 @@ -0,0 +1,393 @@ +#region Bootstrap: NuGet + PowerShellGet + +function Initialize-NuGetProvider { + [CmdletBinding()] + param() + + #region Bootstrap: Defaults / Non-interactive behavior + + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + $ProgressPreference = 'SilentlyContinue' + $ConfirmPreference = 'None' + $PSDefaultParameterValues['*:Confirm'] = $false + + #endregion Bootstrap: Defaults / Non-interactive behavior + + #region Bootstrap: Ensure provider folders exist + + $userProvPath = Join-Path $env:LOCALAPPDATA 'PackageManagement\ProviderAssemblies' + $allProvPath = Join-Path ${env:ProgramFiles} 'PackageManagement\ProviderAssemblies' + + foreach ($p in @($userProvPath, $allProvPath)) { + try { + if ($p -and -not (Test-Path $p)) { + New-Item -Path $p -ItemType Directory -Force -ErrorAction Stop | Out-Null + Write-LogHybrid "Ensured provider folder exists: $p" Info Bootstrap -LogToEvent + } + } + catch { + # AllUsers path can fail without admin. That's OK. + Write-LogHybrid "Could not create provider folder: $p ($($_.Exception.Message))" Warning Bootstrap -LogToEvent + } + } + + #endregion Bootstrap: Ensure provider folders exist + + #region Bootstrap: NuGet provider install/import + + try { + $existing = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue | + Sort-Object Version -Descending | Select-Object -First 1 + + if (-not $existing -or $existing.Version -lt [Version]'2.8.5.201') { + Write-LogHybrid "Installing NuGet provider (min 2.8.5.201)..." Info Bootstrap -LogToEvent + + Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 ` + -Force -ForceBootstrap -Confirm:$false ` + -Scope CurrentUser -ErrorAction Stop | Out-Null + + $existing = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue | + Sort-Object Version -Descending | Select-Object -First 1 + } + + if ($existing) { + Import-PackageProvider -Name NuGet -Force -ErrorAction Stop | Out-Null + Write-LogHybrid "NuGet provider ready (v$($existing.Version))" Success Bootstrap -LogToEvent + } + else { + throw "NuGet provider still not available after install attempt." + } + } + catch { + Write-LogHybrid "NuGet provider install/import failed: $($_.Exception.Message)" Error Bootstrap -LogToEvent + throw + } + + #endregion Bootstrap: NuGet provider install/import + + #region Bootstrap: PackageManagement / PowerShellGet / PSGallery policy + + try { + Import-Module PackageManagement -Force -ErrorAction SilentlyContinue | Out-Null + Import-Module PowerShellGet -Force -ErrorAction SilentlyContinue | Out-Null + + $pkgMgmtVersion = (Get-Module PackageManagement -ListAvailable | + Sort-Object Version -Descending | Select-Object -First 1).Version + + if ($pkgMgmtVersion -and $pkgMgmtVersion -lt [Version]'1.3.1') { + Install-Module PackageManagement -Force -AllowClobber -Confirm:$false -ErrorAction Stop + Write-LogHybrid "Updated PackageManagement to latest version" Info Bootstrap -LogToEvent + } + + if (-not (Get-Module PowerShellGet -ListAvailable)) { + Install-Module PowerShellGet -Force -AllowClobber -Confirm:$false -ErrorAction Stop + Write-LogHybrid "Installed PowerShellGet module" Info Bootstrap -LogToEvent + } + + $gallery = Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue + if ($gallery -and $gallery.InstallationPolicy -ne 'Trusted') { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction Stop + Write-LogHybrid "PSGallery marked as Trusted" Info Bootstrap -LogToEvent + } + } + catch { + Write-LogHybrid "PackageManagement/PowerShellGet setup failed: $($_.Exception.Message)" Warning Bootstrap -LogToEvent + # Decide if you want to throw here or allow soft-fail. + } + + #endregion Bootstrap: PackageManagement / PowerShellGet / PSGallery policy +} + +#endregion Bootstrap: NuGet + PowerShellGet + +#region Registry: Low-level helpers + +function Set-RegistryValueInHkuRoot { + [CmdletBinding()] + param( + [Parameter(Mandatory)] [string] $HkuRoot, + [Parameter(Mandatory)] [string] $RelativeKeyPath, + [Parameter(Mandatory)] [string] $Name, + [Parameter(Mandatory)] + [ValidateSet('String','ExpandString','DWord','QWord','Binary','MultiString')] + [string] $Type, + [Parameter(Mandatory)] $Value + ) + + $k = Join-Path $HkuRoot $RelativeKeyPath + if (-not (Test-Path $k)) { New-Item -Path $k -Force | Out-Null } + + New-ItemProperty -Path $k -Name $Name -PropertyType $Type -Value $Value -Force -ErrorAction Stop | Out-Null +} + +#endregion Registry: Low-level helpers + +#region Registry: Apply settings to Current User + Default User + Existing Profiles + +function Set-RegistryValueForCurrentAndAllUsers { + [CmdletBinding()] + param( + [Parameter(Mandatory)] [string] $RelativeKeyPath, + [Parameter(Mandatory)] [string] $Name, + [Parameter(Mandatory)] + [ValidateSet('String','ExpandString','DWord','QWord','Binary','MultiString')] + [string] $Type, + [Parameter(Mandatory)] $Value + ) + + #region HKCU: current user (best-effort) + + try { + $hkcuKey = "HKCU:\$RelativeKeyPath" + if (-not (Test-Path $hkcuKey)) { New-Item -Path $hkcuKey -Force | Out-Null } + New-ItemProperty -Path $hkcuKey -Name $Name -PropertyType $Type -Value $Value -Force | Out-Null + } + catch { + # Common during SYSTEM runs; ignore + } + + #endregion HKCU: current user (best-effort) + + #region Default User: future profiles + + $defaultDat = "C:\Users\Default\NTUSER.DAT" + $mountName = "SVS_DefaultUser" + $mount = "HKU\$mountName" + $didLoad = $false + + if (Test-Path $defaultDat) { + + if (-not (Test-Path "Registry::HKEY_USERS\$mountName")) { + $loadOut = & reg.exe load $mount $defaultDat 2>&1 + if ($LASTEXITCODE -eq 0) { + $didLoad = $true + Write-LogHybrid "Loaded Default User hive ($defaultDat) to HKEY_USERS\$mountName" Info Tweaks -LogToEvent + } + else { + Write-LogHybrid "Failed to load Default User hive ($defaultDat) to HKEY_USERS\$mountName. reg.exe said: $loadOut" Warning Tweaks -LogToEvent + } + } + else { + Write-LogHybrid "Default User hive already loaded at HKEY_USERS\$mountName (skipping reg load)" Info Tweaks -LogToEvent + } + + try { + if (Test-Path "Registry::HKEY_USERS\$mountName") { + Set-RegistryValueInHkuRoot -HkuRoot "Registry::HKEY_USERS\$mountName" ` + -RelativeKeyPath $RelativeKeyPath -Name $Name -Type $Type -Value $Value + + Write-LogHybrid "Default User updated: [$RelativeKeyPath] $Name = $Value ($Type)" Success Tweaks -LogToEvent + } + } + finally { + if ($didLoad) { + $unloadOut = & reg.exe unload $mount 2>&1 + if ($LASTEXITCODE -eq 0) { + Write-LogHybrid "Unloaded Default User hive from HKEY_USERS\$mountName" Info Tweaks -LogToEvent + } + else { + Write-LogHybrid "Failed to unload Default User hive from HKEY_USERS\$mountName. reg.exe said: $unloadOut" Warning Tweaks -LogToEvent + } + } + } + } + else { + Write-LogHybrid "Default User hive not found at $defaultDat (skipping future-user tweak)" Warning Tweaks -LogToEvent + } + + #endregion Default User: future profiles + + #region Existing Profiles: iterate ProfileList, mount NTUSER.DAT as needed + + $profileList = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" + Get-ChildItem $profileList -ErrorAction SilentlyContinue | ForEach-Object { + $sid = $_.PSChildName + if ($sid -notmatch '^S-1-5-21-\d+-\d+-\d+-\d+$') { return } + + if (Test-Path "Registry::HKEY_USERS\$sid") { + try { + Set-RegistryValueInHkuRoot -HkuRoot "Registry::HKEY_USERS\$sid" ` + -RelativeKeyPath $RelativeKeyPath -Name $Name -Type $Type -Value $Value + } + catch { } + return + } + + $profilePath = (Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue).ProfileImagePath + if (-not $profilePath) { return } + + $ntuser = Join-Path $profilePath "NTUSER.DAT" + if (-not (Test-Path $ntuser)) { return } + + $tempMount = "HKU\SVS_$sid" + & reg.exe load $tempMount $ntuser 2>$null | Out-Null + try { + Set-RegistryValueInHkuRoot -HkuRoot "Registry::HKEY_USERS\SVS_$sid" ` + -RelativeKeyPath $RelativeKeyPath -Name $Name -Type $Type -Value $Value + } + finally { + & reg.exe unload $tempMount 2>$null | Out-Null + } + } + + #endregion Existing Profiles: iterate ProfileList, mount NTUSER.DAT as needed +} + +#endregion Registry: Apply settings to Current User + Default User + Existing Profiles + +#region Shell: Restart helpers + +function Restart-ExplorerIfInteractive { + [CmdletBinding()] + param() + + if ($env:USERNAME -ne 'SYSTEM') { + Stop-Process -Name explorer -Force -ErrorAction SilentlyContinue + Start-Process explorer.exe + } +} + +#endregion Shell: Restart helpers + +#region UI Handlers: Apps + +function Invoke-Install1Password { + param($Context) + + try { + $selected = @('desktop') + + if ($Context -and $Context.Request -and $Context.Request.HttpMethod -eq 'POST') { + $raw = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd() + if (-not [string]::IsNullOrWhiteSpace($raw)) { + $data = $raw | ConvertFrom-Json + if ($data.checkedValues) { $selected = @($data.checkedValues) } + } + } + + $chromeExtId = 'aeblfdkhhhdcdjpifhhbdiojplfjncoa' + $edgeExtId = 'dppgmdbiimibapkepcbdbmkaabgiofem' + + if ($selected -contains 'desktop') { + winget install -e --id AgileBits.1Password --silent --accept-package-agreements --accept-source-agreements + Write-LogHybrid "Installed 1Password desktop app via winget" Success SVSApps -LogToEvent + } + + if ($selected -contains 'chromeExt') { + $chromeKey = "HKLM:\SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist" + New-Item -Path $chromeKey -Force | Out-Null + New-ItemProperty -Path $chromeKey -Name "1" -PropertyType String -Force ` + -Value "$chromeExtId;https://clients2.google.com/service/update2/crx" | Out-Null + Write-LogHybrid "Forced 1Password extension install for Chrome" Success SVSApps -LogToEvent + } + + if ($selected -contains 'edgeExt') { + $edgeKey = "HKLM:\SOFTWARE\Policies\Microsoft\Edge\ExtensionInstallForcelist" + New-Item -Path $edgeKey -Force | Out-Null + New-ItemProperty -Path $edgeKey -Name "1" -PropertyType String -Force ` + -Value "$edgeExtId;https://edge.microsoft.com/extensionwebstorebase/v1/crx" | Out-Null + Write-LogHybrid "Forced 1Password extension install for Edge" Success SVSApps -LogToEvent + } + + Send-Text $Context "1Password processed: $($selected -join ', ')" + } + catch { + Write-LogHybrid "1Password install failed: $($_.Exception.Message)" Error SVSApps -LogToEvent + Send-Text $Context "ERROR: $($_.Exception.Message)" + } +} + +#endregion UI Handlers: Apps + +#region UI Handlers: Tweaks + +function Invoke-DisableAnimations { + param($Context) + + try { + $selected = @() + + if ($Context -and $Context.Request -and $Context.Request.HttpMethod -eq 'POST') { + $raw = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd() + if (-not [string]::IsNullOrWhiteSpace($raw)) { + $data = $raw | ConvertFrom-Json + if ($data.checkedValues) { $selected = @($data.checkedValues) } + } + } + + if ($selected.Count -eq 0) { + $selected = @('window','taskbar','menus') + } + + if ($selected -contains 'window') { + Set-RegistryValueForCurrentAndAllUsers ` + -RelativeKeyPath "Control Panel\Desktop\WindowMetrics" ` + -Name "MinAnimate" -Type String -Value "0" + } + + if ($selected -contains 'taskbar') { + Set-RegistryValueForCurrentAndAllUsers ` + -RelativeKeyPath "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" ` + -Name "TaskbarAnimations" -Type DWord -Value 0 + } + + if ($selected -contains 'menus') { + Set-RegistryValueForCurrentAndAllUsers ` + -RelativeKeyPath "Control Panel\Desktop" ` + -Name "MenuShowDelay" -Type String -Value "50" + } + + if ($selected -contains 'taskbar') { + Restart-ExplorerIfInteractive + } + + Write-LogHybrid "Disable Animations applied (Current + All Existing + Default User). Selected: $($selected -join ', ')" Success Tweaks -LogToEvent + Send-Text $Context "Disable Animations applied: $($selected -join ', ')" + } + catch { + Write-LogHybrid "Disable Animations failed: $($_.Exception.Message)" Error Tweaks -LogToEvent + Send-Text $Context "ERROR: $($_.Exception.Message)" + } +} + +function Invoke-EnableNumLock { + param($Context) + + try { + $path = "Registry::HKEY_USERS\.DEFAULT\Control Panel\Keyboard" + New-Item -Path $path -Force | Out-Null + + # Ensure it's a string value (Windows uses string here) + New-ItemProperty -Path $path -Name "InitialKeyboardIndicators" -PropertyType String -Value "2" -Force | Out-Null + + Write-LogHybrid "NumLock default enabled (HKEY_USERS\.DEFAULT)" Success Tweaks -LogToEvent + Send-Text $Context "NumLock default enabled." + } + catch { + Write-LogHybrid "Enable NumLock failed: $($_.Exception.Message)" Error Tweaks -LogToEvent + Send-Text $Context "ERROR: $($_.Exception.Message)" + } +} + +function Invoke-ClassicContextMenu { + param($Context) + + try { + $key = "HKCU:\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32" + New-Item -Path $key -Force | Out-Null + + Set-ItemProperty -Path $key -Name "(default)" -Value "" -Force + + Stop-Process -Name explorer -Force -ErrorAction SilentlyContinue + Start-Process explorer.exe + + Write-LogHybrid "Classic context menu enabled (Win11)" Success Tweaks -LogToEvent + Send-Text $Context "Classic context menu enabled (Explorer restarted)." + } + catch { + Write-LogHybrid "Classic context menu tweak failed: $($_.Exception.Message)" Error Tweaks -LogToEvent + Send-Text $Context "ERROR: $($_.Exception.Message)" + } +} + +#endregion UI Handlers: Tweaks