diff --git a/Config/ProjectSettings.ps1 b/Config/ProjectSettings.ps1 index 7d1555ab..ec67bdb2 100644 --- a/Config/ProjectSettings.ps1 +++ b/Config/ProjectSettings.ps1 @@ -581,7 +581,7 @@ if (!(Get-Variable -Name CheckReadOnlyVariables -Scope Global -ErrorAction Ignor # Set to false to avoid checking system and environment requirements # This will also disable checking for modules and required services - New-Variable -Name ProjectCheck -Scope Global -Option ReadOnly -Value $false + New-Variable -Name ProjectCheck -Scope Global -Option ReadOnly -Value $true # Set to false to avoid checking if modules are up to date # Enabling this make sense only for development or code navigation diff --git a/Modules/Ruleset.Initialize/Public/Initialize-Provider.ps1 b/Modules/Ruleset.Initialize/Public/Initialize-Provider.ps1 index f6295f3c..6264d66b 100644 --- a/Modules/Ruleset.Initialize/Public/Initialize-Provider.ps1 +++ b/Modules/Ruleset.Initialize/Public/Initialize-Provider.ps1 @@ -198,8 +198,25 @@ function Initialize-Provider # NOTE: If Nuget is not installed Windows PowerShell will ask to install it here and if accepted # $AllProviders will be initialized to downloaded and installed provider # TODO: Bootstraping should be handled manually - [Microsoft.PackageManagement.Packaging.SoftwareIdentity[]] $AllProviders = Find-PackageProvider -Name $ProviderName ` - -RequiredVersion $RequiredVersion -IncludeDependencies -ErrorAction SilentlyContinue + try + { + [Microsoft.PackageManagement.Packaging.SoftwareIdentity[]] $AllProviders = Find-PackageProvider -Name $ProviderName ` + -RequiredVersion $RequiredVersion -IncludeDependencies -ErrorAction Stop + } + catch + { + $InstalledPackage = Get-PackageProvider -Name $ProviderName -ErrorAction SilentlyContinue + $InstalledPackageVersion = $InstalledPackage | Select-Object -ExpandProperty Version + + # Grater than because we do not specify revision in $RequiredVersion which is initialized to -1 + if ($InstalledPackageVersion -and ($InstalledPackageVersion -gt $RequiredVersion)) + { + # NOTE: On Windows 11 system if user chooses to accept downloading the package with + # Find-PackageProvider above it won't initialize $AllProviders but the package may be installed + # This was the case with NuGet on fresh system with out of date NuGet provider + $AllProviders = $InstalledPackage + } + } # If provider was found with Find-PackageProvider it should be installed with Install-PackageProvider $UseInstallPackageProvider = $true @@ -390,9 +407,13 @@ function Initialize-Provider # If package source for "FoundProvider" is not registered do nothing, this will be the cause with # "Bootstrap" provider, which means NuGet was already installed during "Find-PackageProvider" above! # It may also be the case if a user denied registering a package source. - if ($FoundPackageSource.ProviderName -notin (Get-PackageSource).ProviderName) + if (!$FoundPackageSource -or ($FoundPackageSource.ProviderName -notin (Get-PackageSource).ProviderName)) { - Write-Warning -Message "[$($MyInvocation.InvocationName)] Not using '$($FoundPackageSource.ProviderName)' provider to install package, package source not registered" + # Else warning was diplayed by Get-PackageSource + if ($FoundPackageSource) + { + Write-Warning -Message "[$($MyInvocation.InvocationName)] Not using '$($FoundPackageSource.ProviderName)' provider to install package, package source not registered" + } # TODO: This scenario needs testing, it currently works for NuGet in Windows PowerShell return $null -ne (Get-PackageProvider -Name $ProviderName) @@ -553,7 +574,6 @@ function Initialize-Provider return $false } - return $true } else diff --git a/Modules/Ruleset.Initialize/Public/Uninstall-DuplicateModule.ps1 b/Modules/Ruleset.Initialize/Public/Uninstall-DuplicateModule.ps1 index eaa1818e..50ae4b8b 100644 --- a/Modules/Ruleset.Initialize/Public/Uninstall-DuplicateModule.ps1 +++ b/Modules/Ruleset.Initialize/Public/Uninstall-DuplicateModule.ps1 @@ -207,9 +207,10 @@ function Uninstall-DuplicateModule } catch { + Write-Warning -Message "[$($MyInvocation.InvocationName)] Please close down all PowerShell sessions including VSCode, then try again in fresh console" + Write-Error -Category ResourceBusy -TargetObject $LoadedModule ` -Message "Module '$ModuleName' could not be removed from current PS session which is required for uninstallation because: $($_.Exception.Message)" - Write-Warning -Message "[$($MyInvocation.InvocationName)] Please close down all other PowerShell sessions including VSCode, then try again" continue } } @@ -237,14 +238,17 @@ function Uninstall-DuplicateModule } catch { - Write-Error -Category OperationStopped -TargetObject $ModuleRoot ` - -Message "Module directory '$ModuleRoot' could not be recursively removed because: $($_.Exception.Message)" - if ($ModuleRoot -like "$($pwd.Path)*") { - Write-Information -Tags $MyInvocation.InvocationName ` - -MessageData "INFO: This session's prompt is inside module path, the prompt must leave module path $($pwd.Path)" + Write-Warning -Message "[$($MyInvocation.InvocationName)] This session's prompt is inside module path, the prompt must leave module path $($pwd.Path)" } + else + { + Write-Warning -Message "[$($MyInvocation.InvocationName)] please close down other PowerShell sessions including VSCode, then try again in fresh console" + } + + Write-Error -Category OperationStopped -TargetObject $ModuleRoot ` + -Message "Module directory '$ModuleRoot' could not be recursively removed because: $($_.Exception.Message)" continue } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 7322f3a0..499e00bd 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -43,6 +43,8 @@ Here is a list of changes for each of the releases. - `Get-SystemSoftware` renamed to `Get-SystemProgram` - `Get-UserSoftware` renamed to `Get-UserProgram` - `Get-SystemProgram` Improved program search + - `Uninstall-DuplicateModule` Improved to show reason for failure and how to fix + - `Initialize-Provider` Was failing in Windows PS on Windows 11, fixed - Scripts