[Bug] cleanup.ps1 script is removing the latest version
Environmental information
#### PowerShell version:
Name Value
---- -----
PSVersion 5.1.18362.628
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.628
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
#### dbatools Module version:
Name : dbatools
Path : C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.104\dbatools.psd1
Version : 1.0.104
Name : dbatools
Path : C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.101\dbatools.psd1
Version : 1.0.101
SQL Server:
Not Applicable
Report
As of 1.0.104, the cleanup.ps1 script is deleting itself (the whole current module) along with the old versions.
Host used
- [X] powershell.exe
- [ ] ISE
- [ ] VS Code
- [ ] Other (please specify)
If anything other than powershell.exe was used, please confirm that you can duplicate the issue with powershell.exe
- [ ] Still buggy in powershell.exe
Errors Received
Steps to Reproduce
Have two or more versions of dbatools installed
Install-Module -Name dbatools -RequiredVersion 1.0.101
Install-Module -Name dbatools -RequiredVersion 1.0.104
Then run the latest cleanup.ps1 from 1.0.104
& "$((Get-InstalledModule dbatools).InstalledLocation)\cleanup.ps1"
Expected Behavior
Remove 1.0.101 but leave 1.0.104
Actual Behavior
Both 1.0.101 and 1.0.104 are gone, leaving no dbatools :(
I think this relates to PR #6447. Something seems off with the semantic versioning comparison. I ran this through the debugger and the problem seems to be at line 39
$olderVersions = $installedVersion | Where-Object [version]Version -lt [version]$newestVersion.Version
The Where-Object is returning both 1.0.101 and 1.0.104 in this example and then they get deleted.
@ben-thul I think made that changed and recall had tested it, showing it worked.
#6447 for reference.
Hi @ben-thul - I saw that PR and mentioned in the report. I was able to reproduce this behavior on a couple different Windows machines. Install two or more versions including 104, run cleanup, removes all versions of dbatools.
So it is not putting the latest back on the machine?
Correct, it doesn't go into the elseif that does the Install-Module. This is for the scenario where latest version is found and older versions are also found.
I had some time to debug this further today. Added a fix in PR#6462 and posted a bunch of details in there.