PowerShellGetv2
PowerShellGetv2 copied to clipboard
Updating PackageManagement Module in PowerShell Core - Assembly Name Conflicts
I have a little script that I'm working on that attempts to update the PackageManagement and PowerShellGet Modules in PowerShell Core if they're not the latest. It seems like I'm getting a race condition in PowerShell Core beta.6 and 7 when I attempt to update the PackageManagement Module.
Here's a Git Gist with both the script (~100 lines) and the resulting errors I get on PowerShell Core beta.6 and 7 (any OS):
https://gist.github.com/pldmgg/b8db54742d511bd7d8d5742d539ebbe1
The results of my testing have shown that:
-
It works fine on Windows PowerShell 5.1
-
It throws errors as seen in the above Gist in PowerShell Core beta.6 and 7 on any OS.
-
After the errors are thrown, using 'Get-Module' shows that the latest version of the PackageManagement Module is loaded, but there aren't any ExportedCommands
-
If I start a new PowerShell Core Session after experiencing the above errors and try the script again, everything works fine
So, the errors are pretty clear that there's a conflict with assembly names that are currently loaded / trying to be loaded, and I understand that the reason that starting a new PowerShell Core Session works is because that's the only way to unload previously loaded assemblies, but maybe there's a way to have PackageManagement.psm1 check to see if certain needed assemblies are already loaded before trying to load them again in PowerShell Core?
@pldmgg Yes, it is required to start a new PowerShell Session to load the newly installed versions of PackageManagement and PowerShellGet modules. Please take a look at 'Get the latest version from PowerShell Gallery' section in doc.
I wouldn't really consider this resolved.
To be clear, according to the output of 'Get-Module' for a brand new session in both Windows PowerShell and PowerShell Core, neither PackageManagement nor PowerShellGet Modules are loaded, which means none of their respective assemblies are loaded (I assume).
My script (around line 32) uses the "Find-Module" cmdlet (part of PowerShellGet), which automatically loads the latest locally available versions of PowerShellGet and its dependency PackageManagement. The versions of these Modules that are loaded at the time Find-Module is called are NOT the latest versions according to the output of Find-Module.
From there, the latest version(s) are installed. The difference in behavior between Windows PowerShell and PowerShell Core comes with lines 80-105, where the old version(s) of PowerShellGet and PackageManagement are removed via the Remove-Module cmdlet, and then the newly installed latest version(s) are imported using the Import-Module cmdlet.
Windows PowerShell has no problem importing the latest version(s) of the modules within the same session (after having removed the older versions).
PowerShell Core, on the other hand, throws the aforementioned (in the Gist) errors when trying to import the latest versions after having removed the older ones within the same session.
So, at the end of the day, Windows PowerShell can update within the same session, and PowerShell Core cannot. Maybe the behavior of Import-Module / Remove-Module is different between Windows PowerShell and PowerShell Core, in that Windows PowerShell's Remove-Module actually removes referenced assemblies?
My workaround for this was to, before loading any modules, update package management "manually" by pulling it from the PSGallery URI and unzipping it in-place before importing it, this way the "new" DLL is the first thing that loads, because if you try to use Find-Module or update-module as mentioned, it will load the DLL which will be too late.