From 8aa297cf4726bb0bf7c114a2cefc15e6fb8148cc Mon Sep 17 00:00:00 2001 From: Adam Kreiss Date: Tue, 26 Nov 2019 14:28:04 -0500 Subject: [PATCH 1/7] Added support for upgrading lower versions of a package instead of uninstalling/reinstalling --- .../cChocoPackageInstall.psm1 | 40 +++++++++++++++---- .../cChocoPackageInstall.schema.mof | 1 + 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 539845a..c01fdd1 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -80,7 +80,9 @@ function Set-TargetResource [String] $chocoParams, [bool] - $AutoUpgrade = $false + $AutoUpgrade = $false, + [bool] + $UpgradeLowerVersions = $false ) Write-Verbose -Message 'Start Set-TargetResource' $isVersionPresent = $PSBoundParameters.ContainsKey('Version') @@ -117,10 +119,25 @@ function Set-TargetResource $whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Installing / upgrading package from Chocolatey') if ($whatIfShouldProcess) { if ($Version) { - Write-Verbose -Message "Uninstalling $Name due to version mis-match" - UninstallPackage -pName $Name -pParams $Params - Write-Verbose -Message "Re-Installing $Name with correct version $versionToInstall" - InstallPackage -pName $Name -pParams $Params -pVersion $versionToInstall -pSource $Source -cParams $chocoParams + # Version check on the existing package to ensure we don't attempt to upgrade in a downgrade situation + $installedPackages = @(Get-ChocoInstalledPackage | Where-object { $_.Name -eq $Name }) + $packageCount = $installedPackages.Count + Write-Verbose "Found $packageCount matching package(s) for upgrade" + $canUpgrade = $packageCount -eq 1 + if ($canUpgrade) { + [version] $installedVersion = $installedPackages[0].Version + $canUpgrade = $installedVersion.CompareTo([version]$Version) -le 0 + } + if ($UpgradeLowerVersions -and $canUpgrade) { + Write-Verbose -Message "Upgrading $Name to version $Version" + Upgrade-Package -pName $Name -pParams $Params -pVersion $Version + } + else { + Write-Verbose -Message "Uninstalling $Name due to version mis-match" + UninstallPackage -pName $Name -pParams $Params + Write-Verbose -Message "Re-Installing $Name with correct version $version" + InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams + } } elseif ($MinimumVersion) { Write-Verbose -Message "Upgrading $Name because installed version is lower that the specified minimum" @@ -169,7 +186,9 @@ function Test-TargetResource [String] $chocoParams, [bool] - $AutoUpgrade = $false + $AutoUpgrade = $false, + [bool] + $UpgradeLowerVersions = $false ) Write-Verbose -Message 'Start Test-TargetResource' @@ -443,7 +462,7 @@ function global:Write-Host Write-Verbose -Message $Object } -Function Upgrade-Package { +function Upgrade-Package { [Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs','')] [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] param( @@ -452,8 +471,10 @@ Function Upgrade-Package { [Parameter(Position=1)] [string]$pParams, [Parameter(Position=2)] - [string]$pSource, + [string]$pVersion, [Parameter(Position=3)] + [string]$pSource, + [Parameter(Position=4)] [string]$cParams ) @@ -464,6 +485,9 @@ Function Upgrade-Package { if ($pParams) { $chocoParams += " --params=`"$pParams`"" } + if ($pVersion) { + $chocoinstallparams += " --version=`"$pVersion`"" + } if ($pSource) { $chocoParams += " --source=`"$pSource`"" } diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof index 03b8e62..bbe40bf 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof @@ -9,4 +9,5 @@ class cChocoPackageInstall : OMI_BaseResource [write] string Source; [Write] String chocoParams; [Write] Boolean AutoUpgrade; + [Write] Boolean UpgradeLowerVersions; }; From d19da2c83a4d981334284889708f4d439908c844 Mon Sep 17 00:00:00 2001 From: Adam Kreiss Date: Tue, 26 Nov 2019 15:02:25 -0500 Subject: [PATCH 2/7] Fixed bad variable name --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index c01fdd1..2d6f413 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -486,7 +486,7 @@ function Upgrade-Package { $chocoParams += " --params=`"$pParams`"" } if ($pVersion) { - $chocoinstallparams += " --version=`"$pVersion`"" + $chocoupgradeparams += " --version=`"$pVersion`"" } if ($pSource) { $chocoParams += " --source=`"$pSource`"" From 320ba4eeadaf458121e27507687cfb1b33cbec39 Mon Sep 17 00:00:00 2001 From: Adam Kreiss Date: Wed, 27 Nov 2019 09:59:30 -0500 Subject: [PATCH 3/7] Fixing variable name change from merge --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 2d6f413..656390d 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -486,7 +486,7 @@ function Upgrade-Package { $chocoParams += " --params=`"$pParams`"" } if ($pVersion) { - $chocoupgradeparams += " --version=`"$pVersion`"" + $chocoParams += " --version=`"$pVersion`"" } if ($pSource) { $chocoParams += " --source=`"$pSource`"" From 391091d8ee87672e651f1e155fcfeae4ed3e7b26 Mon Sep 17 00:00:00 2001 From: Adam Kreiss Date: Thu, 14 May 2020 16:40:22 -0400 Subject: [PATCH 4/7] Corrected use of [Version] in comparision of choco package versions --- .../cChocoPackageInstall.psm1 | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 656390d..0e92fe9 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -82,7 +82,7 @@ function Set-TargetResource [bool] $AutoUpgrade = $false, [bool] - $UpgradeLowerVersions = $false + $UpgradeLowerVersion = $false ) Write-Verbose -Message 'Start Set-TargetResource' $isVersionPresent = $PSBoundParameters.ContainsKey('Version') @@ -123,12 +123,15 @@ function Set-TargetResource $installedPackages = @(Get-ChocoInstalledPackage | Where-object { $_.Name -eq $Name }) $packageCount = $installedPackages.Count Write-Verbose "Found $packageCount matching package(s) for upgrade" - $canUpgrade = $packageCount -eq 1 + $canUpgrade = $packageCount -eq 1 if ($canUpgrade) { - [version] $installedVersion = $installedPackages[0].Version - $canUpgrade = $installedVersion.CompareTo([version]$Version) -le 0 + [Version] $installedVersion = Get-VersionCore -Version $installedPackages[0].Version + [Version] $targetVersion = Get-VersionCore -Version $Version + + $canUpgrade = $installedVersion.CompareTo($targetVersion) -le 0 } - if ($UpgradeLowerVersions -and $canUpgrade) { + + if ($UpgradeLowerVersion -and $canUpgrade) { Write-Verbose -Message "Upgrading $Name to version $Version" Upgrade-Package -pName $Name -pParams $Params -pVersion $Version } @@ -188,7 +191,7 @@ function Test-TargetResource [bool] $AutoUpgrade = $false, [bool] - $UpgradeLowerVersions = $false + $UpgradeLowerVersion = $false ) Write-Verbose -Message 'Start Test-TargetResource' @@ -471,11 +474,11 @@ function Upgrade-Package { [Parameter(Position=1)] [string]$pParams, [Parameter(Position=2)] - [string]$pVersion, - [Parameter(Position=3)] [string]$pSource, + [Parameter(Position=3)] + [string]$cParams, [Parameter(Position=4)] - [string]$cParams + [string]$pVersion ) $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') @@ -574,4 +577,18 @@ function Get-ChocoVersion { Return $res } +function Get-VersionCore { + [CmdletBinding()] + param ( + [string]$Version + ) + + [Version] $versionCore = $null + if ($Version -match '(?[1-9]\d*)(\.(?[0-9]*))?(\.(?[0-9]*))?(\.(?[0-9]*))?([-+].*)?') { + $versionCore = New-Object System.Version($Matches.Major, $Matches.Minor, $Matches.Patch, $Matches.Segment) + } + + $versionCore +} + Export-ModuleMember -Function *-TargetResource From b81c7c452b9de31eba48c9c00514c03697c68117 Mon Sep 17 00:00:00 2001 From: Adam Kreiss Date: Thu, 14 May 2020 16:45:39 -0400 Subject: [PATCH 5/7] Missed updating the UpgradeLowerVersion param in the schema file --- .../cChocoPackageInstall/cChocoPackageInstall.schema.mof | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof index bbe40bf..01c8612 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof @@ -9,5 +9,5 @@ class cChocoPackageInstall : OMI_BaseResource [write] string Source; [Write] String chocoParams; [Write] Boolean AutoUpgrade; - [Write] Boolean UpgradeLowerVersions; + [Write] Boolean UpgradeLowerVersion; }; From dd0c3ffccbfe79c4c409f452133cc9c5e7fb2679 Mon Sep 17 00:00:00 2001 From: Adam Kreiss Date: Tue, 9 Jun 2020 16:52:21 -0400 Subject: [PATCH 6/7] Removed UpgradeLowerVersion parameter and switched default functionality to be to upgrade a package --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 10 +++------- .../cChocoPackageInstall.schema.mof | 1 - 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 0e92fe9..bfbd027 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -80,9 +80,7 @@ function Set-TargetResource [String] $chocoParams, [bool] - $AutoUpgrade = $false, - [bool] - $UpgradeLowerVersion = $false + $AutoUpgrade = $false ) Write-Verbose -Message 'Start Set-TargetResource' $isVersionPresent = $PSBoundParameters.ContainsKey('Version') @@ -131,7 +129,7 @@ function Set-TargetResource $canUpgrade = $installedVersion.CompareTo($targetVersion) -le 0 } - if ($UpgradeLowerVersion -and $canUpgrade) { + if ($canUpgrade) { Write-Verbose -Message "Upgrading $Name to version $Version" Upgrade-Package -pName $Name -pParams $Params -pVersion $Version } @@ -189,9 +187,7 @@ function Test-TargetResource [String] $chocoParams, [bool] - $AutoUpgrade = $false, - [bool] - $UpgradeLowerVersion = $false + $AutoUpgrade = $false ) Write-Verbose -Message 'Start Test-TargetResource' diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof index 01c8612..03b8e62 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.schema.mof @@ -9,5 +9,4 @@ class cChocoPackageInstall : OMI_BaseResource [write] string Source; [Write] String chocoParams; [Write] Boolean AutoUpgrade; - [Write] Boolean UpgradeLowerVersion; }; From e6e48f5a30ded9e06cdeaa065f86f07ec448aba5 Mon Sep 17 00:00:00 2001 From: Adam Kreiss Date: Fri, 26 Jun 2020 10:25:16 -0400 Subject: [PATCH 7/7] Added max version for Pester at 4.10.1 --- AppVeyor/AppVeyorInstall.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AppVeyor/AppVeyorInstall.ps1 b/AppVeyor/AppVeyorInstall.ps1 index 51693d8..ae7c4ac 100644 --- a/AppVeyor/AppVeyorInstall.ps1 +++ b/AppVeyor/AppVeyorInstall.ps1 @@ -29,8 +29,9 @@ Write-Host "Installed NuGet version '$($pkg.version)'" # Install Modules # #---------------------------------# [version]$ScriptAnalyzerVersion = '1.8.1' +[version]$PesterVersion = '4.10.1' Install-Module -Name 'PSScriptAnalyzer' -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $ScriptAnalyzerVersion -Install-Module -Name 'Pester' -SkipPublisherCheck -Repository PSGallery -Force -ErrorAction Stop +Install-Module -Name 'Pester' -SkipPublisherCheck -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $PesterVersion Install-Module -Name 'xDSCResourceDesigner' -Repository PSGallery -Force -ErrorAction Stop #---------------------------------#