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