Update samy.functions.ps1

This commit is contained in:
2025-12-21 00:19:19 -05:00
committed by syelle
parent fa307eaceb
commit a73d53130d

View File

@@ -1,81 +1,86 @@
function Initialize-NuGetProvider { function Initialize-NuGetProvider {
[CmdletBinding()] [CmdletBinding()]
param() param()
#region — guarantee NuGet provider is present without prompting # Silent defaults
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
$ConfirmPreference = 'None'
# ─── Silent defaults ─── # Extra guardrails for "ShouldContinue" style prompts
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $PSDefaultParameterValues['*:Confirm'] = $false
$ProgressPreference = 'SilentlyContinue'
$ConfirmPreference = 'None'
# ─── Pre-create folder if running as SYSTEM (avoids NuGet install bug) ─── # Ensure provider folders exist (CurrentUser and AllUsers locations)
$provPath = "$env:ProgramData\PackageManagement\ProviderAssemblies" $userProvPath = Join-Path $env:LOCALAPPDATA 'PackageManagement\ProviderAssemblies'
if (-not (Test-Path $provPath)) { $allProvPath = Join-Path ${env:ProgramFiles} 'PackageManagement\ProviderAssemblies'
try {
New-Item -Path $provPath -ItemType Directory -Force -ErrorAction Stop | Out-Null foreach ($p in @($userProvPath, $allProvPath)) {
Write-LogHybrid "Created missing provider folder: $provPath" Info Bootstrap -LogToEvent try {
} catch { if ($p -and -not (Test-Path $p)) {
Write-LogHybrid "Failed to create provider folder: $($_.Exception.Message)" Warning Bootstrap -LogToEvent 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
}
}
# ─── Ensure PowerShellGet is available ─── # 1) Install NuGet provider FIRST, silently, so Install-Module never prompts
if (-not (Get-Command Install-PackageProvider -ErrorAction SilentlyContinue)) { try {
try { $existing = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue |
Install-Module PowerShellGet -Force -AllowClobber -Confirm:$false -ErrorAction Stop Sort-Object Version -Descending | Select-Object -First 1
Write-LogHybrid "Installed PowerShellGet module" Info Bootstrap -LogToEvent
} catch {
Write-LogHybrid "PowerShellGet install failed: $($_.Exception.Message)" Error Bootstrap -LogToEvent
}
}
# ─── Ensure PackageManagement is up-to-date ─── if (-not $existing -or $existing.Version -lt [Version]'2.8.5.201') {
$pkgMgmtVersion = (Get-Module PackageManagement -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1).Version
if ($pkgMgmtVersion -lt [Version]"1.3.1") {
try {
Install-Module PackageManagement -Force -AllowClobber -Confirm:$false -ErrorAction Stop
Write-LogHybrid "Updated PackageManagement to latest version" Info Bootstrap -LogToEvent
} catch {
Write-LogHybrid "PackageManagement update failed: $($_.Exception.Message)" Warning Bootstrap -LogToEvent
}
}
# ─── Import modules silently ─── Write-LogHybrid "Installing NuGet provider (min 2.8.5.201)..." Info Bootstrap -LogToEvent
Import-Module PackageManagement -Force -ErrorAction SilentlyContinue | Out-Null
Import-Module PowerShellGet -Force -ErrorAction SilentlyContinue | Out-Null
# ─── Trust PSGallery if not already ─── # ForceBootstrap helps avoid interactive bootstrap prompts
$gallery = Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 `
if ($gallery -and $gallery.InstallationPolicy -ne 'Trusted') { -Force -ForceBootstrap -Confirm:$false `
try { -Scope CurrentUser -ErrorAction Stop | Out-Null
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction Stop
Write-LogHybrid "PSGallery marked as Trusted" Info Bootstrap -LogToEvent
} catch {
Write-LogHybrid "Failed to trust PSGallery: $($_.Exception.Message)" Warning Bootstrap -LogToEvent
}
}
# ─── Ensure NuGet is installed silently ─── $existing = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue |
$nuget = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue Sort-Object Version -Descending | Select-Object -First 1
if (-not $nuget) { }
try {
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false -ErrorAction Stop
$nuget = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue
Write-LogHybrid "Installed NuGet provider v$($nuget.Version)" Info Bootstrap -LogToEvent
} catch {
Write-LogHybrid "NuGet install failed: $($_.Exception.Message)" Error Bootstrap -LogToEvent
}
} else {
Write-LogHybrid "NuGet provider already present (v$($nuget.Version))" Info Bootstrap -LogToEvent
}
# ─── Final import check ─── if ($existing) {
try { Import-PackageProvider -Name NuGet -Force -ErrorAction Stop | Out-Null
Import-PackageProvider -Name NuGet -Force -ErrorAction Stop | Out-Null Write-LogHybrid "NuGet provider ready (v$($existing.Version))" Success Bootstrap -LogToEvent
} catch { } else {
Write-LogHybrid "NuGet provider import failed: $($_.Exception.Message)" Error Bootstrap -LogToEvent throw "NuGet provider still not available after install attempt."
} }
}
catch {
Write-LogHybrid "NuGet provider install/import failed: $($_.Exception.Message)" Error Bootstrap -LogToEvent
throw
}
#endregion — guarantee NuGet provider is present without prompting # 2) Now it is safe to update / install modules without NuGet prompts
} 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
}
# Trust PSGallery (optional, but common)
$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
# You can choose to throw here if you want hard-fail behavior
}
}