From c8f5a1997796dfb780c86f86eaee3de071509ca7 Mon Sep 17 00:00:00 2001 From: jrdnr Date: Sun, 3 Dec 2017 16:09:03 -0500 Subject: [PATCH 1/9] Choco Versions >10.4 overwhelm log with download Progress data Added Check for Choco version and add "--no-progress" to the Params list - Renamed Params variable for consistency across functions - Updated Choco comand execution formatting to be consistent across functions --- .../cChocoPackageInstall.psm1 | 70 ++++++++++++------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 4e1afb4..5709cb5 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -226,23 +226,29 @@ function InstallPackage $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - [string]$chocoinstallparams = '-y' + [string]$chocoParams = '-y' if ($pParams) { - $chocoinstallparams += " --params=`"$pParams`"" + $chocoParams += " --params=`"$pParams`"" } if ($pVersion) { - $chocoinstallparams += " --version=`"$pVersion`"" + $chocoParams += " --version=`"$pVersion`"" } if ($pSource) { - $chocoinstallparams += " --source=`"$pSource`"" + $chocoParams += " --source=`"$pSource`"" } if ($cParams) { - $chocoinstallparams += " $cParams" + $chocoParams += " $cParams" + } + # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress + $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version + if ($chocoVer -ge 0.10.4) { + $chocoParams += " --no-progress" } - Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'" - $packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams" - Write-Verbose -Message "Package output $packageInstallOuput " + $cmd = "choco install $pName $chocoParams" + Write-Verbose -Message "Install command: '$cmd'" + $packageInstallOuput = Invoke-Expression -Command $cmd + Write-Verbose -Message "Package output $packageInstallOuput" # Clear Package Cache Get-ChocoInstalledPackage 'Purge' @@ -262,17 +268,22 @@ function UninstallPackage $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') - #Todo: Refactor - if (-not ($pParams)) - { - Write-Verbose -Message 'Uninstalling Package Standard' - $packageUninstallOuput = choco uninstall $pName -y + [string]$chocoParams = "-y" + if ($pParams) { + $chocoParams += " --params=`"$pParams`"" } - elseif ($pParams) - { - Write-Verbose -Message "Uninstalling Package with params $pParams" - $packageUninstallOuput = choco uninstall $pName --params="$pParams" -y + if ($pVersion) { + $chocoParams += " --version=`"$pVersion`"" } + # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress + $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version + if ($chocoVer -ge 0.10.4) { + $chocoParams += " --no-progress" + } + + $cmd = "choco uninstall $pName $chocoParams" + Write-Verbose -Message "Uninstalling $pName with: '$cmd'" + $packageUninstallOuput = Invoke-Expression -Command $cmd Write-Verbose -Message "Package uninstall output $packageUninstallOuput " @@ -325,14 +336,15 @@ Function Test-LatestVersionInstalled { ) Write-Verbose -Message "Testing if $pName can be upgraded" - [string]$chocoupgradeparams = '--noop' + [string]$chocoParams = '--noop' if ($pSource) { - $chocoupgradeparams += " --source=`"$pSource`"" + $chocoParams += " --source=`"$pSource`"" } - Write-Verbose -Message "Testing if $pName can be upgraded: 'choco upgrade $pName $chocoupgradeparams'" + $cmd = "choco upgrade $pName $chocoParams" + Write-Verbose -Message "Testing if $pName can be upgraded: '$cmd'" - $packageUpgradeOuput = Invoke-Expression -Command "choco upgrade $pName $chocoupgradeparams" + $packageUpgradeOuput = Invoke-Expression -Command $cmd $packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_} if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") { @@ -379,17 +391,23 @@ Function Upgrade-Package { $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') Write-Verbose -Message "Path variables: $env:Path" - [string]$chocoupgradeparams = '-dv -y' + [string]$chocoParams = '-dv -y' if ($pParams) { - $chocoupgradeparams += " --params=`"$pParams`"" + $chocoParams += " --params=`"$pParams`"" } if ($pSource) { - $chocoupgradeparams += " --source=`"$pSource`"" + $chocoParams += " --source=`"$pSource`"" } if ($cParams) { - $chocoupgradeparams += " $cParams" + $chocoParams += " $cParams" } - $cmd = "choco upgrade $pName $chocoupgradeparams" + # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress + $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version + if ($chocoVer -ge 0.10.4) { + $chocoParams += " --no-progress" + } + + $cmd = "choco upgrade $pName $chocoParams" Write-Verbose -Message "Upgrade command: '$cmd'" if (-not (IsPackageInstalled -pName $pName)) From b345453d84f099981c209042771e20c38f998f7b Mon Sep 17 00:00:00 2001 From: jrdnr Date: Sun, 3 Dec 2017 16:24:19 -0500 Subject: [PATCH 2/9] Fix Pester Test run Failure Adds "[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]" to function UninstallPackage, to match other functions that use InvokeExpression to call Chco --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 5709cb5..97636df 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -259,6 +259,7 @@ function InstallPackage function UninstallPackage { + [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] param( [Parameter(Position=0,Mandatory)] [string]$pName, From 971ab35d363d0d015a174830c96ac164e32ca0da Mon Sep 17 00:00:00 2001 From: jrdnr Date: Fri, 8 Dec 2017 13:52:42 -0500 Subject: [PATCH 3/9] Powershell cannot compair 0.10.4 to a version number Powershell needs versions to be specified as strings in order to compair them to version numbers. Updated to '0.10.4' insure accurate logic. --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 97636df..9297c16 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -241,7 +241,7 @@ function InstallPackage } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version - if ($chocoVer -ge 0.10.4) { + if ($chocoVer -ge '0.10.4') { $chocoParams += " --no-progress" } @@ -278,7 +278,7 @@ function UninstallPackage } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version - if ($chocoVer -ge 0.10.4) { + if ($chocoVer -ge '0.10.4') { $chocoParams += " --no-progress" } @@ -404,7 +404,7 @@ Function Upgrade-Package { } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version - if ($chocoVer -ge 0.10.4) { + if ($chocoVer -ge '0.10.4') { $chocoParams += " --no-progress" } From 6eb98994af06dbb246396787c34f9c5ff92cda54 Mon Sep 17 00:00:00 2001 From: jrdnr Date: Mon, 11 Dec 2017 10:24:12 -0500 Subject: [PATCH 4/9] Implemented caching choco version Ensured explicitly casting '0.10.4' to Version --- .../cChocoPackageInstall.psm1 | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 9297c16..1fd22bf 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -240,8 +240,7 @@ function InstallPackage $chocoParams += " $cParams" } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress - $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version - if ($chocoVer -ge '0.10.4') { + if ((Get-ChocoVersion) -ge [version]('0.10.4')){ $chocoParams += " --no-progress" } @@ -277,8 +276,7 @@ function UninstallPackage $chocoParams += " --version=`"$pVersion`"" } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress - $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version - if ($chocoVer -ge '0.10.4') { + if ((Get-ChocoVersion) -ge [version]('0.10.4')){ $chocoParams += " --no-progress" } @@ -403,8 +401,7 @@ Function Upgrade-Package { $chocoParams += " $cParams" } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress - $chocoVer = [system.version](Get-ChocoInstalledPackage | Where-Object { $_.name -eq 'chocolatey' }).Version - if ($chocoVer -ge '0.10.4') { + if ((Get-ChocoVersion) -ge [version]('0.10.4')){ $chocoParams += " --no-progress" } @@ -445,4 +442,26 @@ function Get-ChocoInstalledPackage ($action) { Return $res } +function Get-ChocoVersion ($action) { + $chocoInstallCache = Join-Path -Path $env:ChocolateyInstall -ChildPath 'cache' + if ( -not (Test-Path $chocoInstallCache)){ + New-Item -Name 'cache' -Path $env:ChocolateyInstall -ItemType Directory | Out-Null + } + $chocoVersion = Join-Path -Path $chocoInstallCache -ChildPath 'ChocoVersion.xml' + + if ($action -eq 'Purge') { + Remove-Item $chocoVersion -Force + $res = $true + } else { + $cacheSec = (Get-Date).AddSeconds('-60') + if ( $cacheSec -lt (Get-Item $chocoVersion -ErrorAction SilentlyContinue).LastWriteTime ) { + $res = Import-Clixml $chocoVersion + } else { + $res = [version](choco -v) | Export-Clixml -Path $chocoVersion + } + } + + Return $res +} + Export-ModuleMember -Function *-TargetResource From 97584a643423f91e8d1072903e79071c74f8b25a Mon Sep 17 00:00:00 2001 From: jrdnr Date: Mon, 11 Dec 2017 13:10:44 -0500 Subject: [PATCH 5/9] Choco appends '-beta' to beta version numbers Added parsing to split on '-' and only use the first section. --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 1fd22bf..a43180b 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -455,9 +455,11 @@ function Get-ChocoVersion ($action) { } else { $cacheSec = (Get-Date).AddSeconds('-60') if ( $cacheSec -lt (Get-Item $chocoVersion -ErrorAction SilentlyContinue).LastWriteTime ) { - $res = Import-Clixml $chocoVersion + $res = Import-Clixml $chocoVersion } else { - $res = [version](choco -v) | Export-Clixml -Path $chocoVersion + $cmd = choco -v + $res = [version]($cmd.Split('-')[0]) + $res | Export-Clixml -Path $chocoVersion } } From d75d3f7040608d53f198d6eb810488b6dd883f70 Mon Sep 17 00:00:00 2001 From: jrdnr Date: Tue, 12 Dec 2017 12:12:09 -0500 Subject: [PATCH 6/9] Updated [Version] to Fully Qualified [System.Version] --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index a43180b..0c9b22a 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -240,7 +240,7 @@ function InstallPackage $chocoParams += " $cParams" } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress - if ((Get-ChocoVersion) -ge [version]('0.10.4')){ + if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){ $chocoParams += " --no-progress" } @@ -276,7 +276,7 @@ function UninstallPackage $chocoParams += " --version=`"$pVersion`"" } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress - if ((Get-ChocoVersion) -ge [version]('0.10.4')){ + if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){ $chocoParams += " --no-progress" } @@ -401,7 +401,7 @@ Function Upgrade-Package { $chocoParams += " $cParams" } # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress - if ((Get-ChocoVersion) -ge [version]('0.10.4')){ + if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){ $chocoParams += " --no-progress" } @@ -458,7 +458,7 @@ function Get-ChocoVersion ($action) { $res = Import-Clixml $chocoVersion } else { $cmd = choco -v - $res = [version]($cmd.Split('-')[0]) + $res = [System.Version]($cmd.Split('-')[0]) $res | Export-Clixml -Path $chocoVersion } } From d6d0275a7f69921dff440022d934e03a86a2ac9a Mon Sep 17 00:00:00 2001 From: jrdnr Date: Fri, 14 Dec 2018 22:15:55 -0500 Subject: [PATCH 7/9] Converted simple functions to "Advanced Functions" to match the rest of code --- .../cChocoPackageInstall.psm1 | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 0c9b22a..79c2000 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -250,7 +250,7 @@ function InstallPackage Write-Verbose -Message "Package output $packageInstallOuput" # Clear Package Cache - Get-ChocoInstalledPackage 'Purge' + Get-ChocoInstalledPackage -Purge #refresh path varaible in powershell, as choco doesn"t, to pull in git $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') @@ -287,7 +287,7 @@ function UninstallPackage Write-Verbose -Message "Package uninstall output $packageUninstallOuput " # Clear Package Cache - Get-ChocoInstalledPackage 'Purge' + Get-ChocoInstalledPackage -Purge #refresh path varaible in powershell, as choco doesn"t, to pull in git $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') @@ -417,42 +417,54 @@ Function Upgrade-Package { $packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ } # Clear Package Cache - Get-ChocoInstalledPackage 'Purge' + Get-ChocoInstalledPackage -Purge } -function Get-ChocoInstalledPackage ($action) { +function Get-ChocoInstalledPackage { + [CmdletBinding()] + param ( + [switch]$Purge, + [switch]$NoCache + ) + $ChocoInstallLP = Join-Path -Path $env:ChocolateyInstall -ChildPath 'cache' if ( -not (Test-Path $ChocoInstallLP)){ New-Item -Name 'cache' -Path $env:ChocolateyInstall -ItemType Directory | Out-Null } $ChocoInstallList = Join-Path -Path $ChocoInstallLP -ChildPath 'ChocoInstalled.xml' - if ($action -eq 'Purge') { - Remove-Item $ChocoInstallList -Force - $res = $true - } else { + if ( -not $Purge) { $PackageCacheSec = (Get-Date).AddSeconds('-60') if ( $PackageCacheSec -lt (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime ) { $res = Import-Clixml $ChocoInstallList } else { - choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|" -OutVariable res | Export-Clixml -Path $ChocoInstallList + $res = choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|" + if ( -not $NoCache){ + $res | Export-Clixml -Path $ChocoInstallList + } } + } else { + Remove-Item $ChocoInstallList -Force + $res = $true } Return $res } -function Get-ChocoVersion ($action) { +function Get-ChocoVersion { + [CmdletBinding()] + param ( + [switch]$Purge, + [switch]$NoCache + ) + $chocoInstallCache = Join-Path -Path $env:ChocolateyInstall -ChildPath 'cache' if ( -not (Test-Path $chocoInstallCache)){ New-Item -Name 'cache' -Path $env:ChocolateyInstall -ItemType Directory | Out-Null } $chocoVersion = Join-Path -Path $chocoInstallCache -ChildPath 'ChocoVersion.xml' - if ($action -eq 'Purge') { - Remove-Item $chocoVersion -Force - $res = $true - } else { + if ( -not $Purge) { $cacheSec = (Get-Date).AddSeconds('-60') if ( $cacheSec -lt (Get-Item $chocoVersion -ErrorAction SilentlyContinue).LastWriteTime ) { $res = Import-Clixml $chocoVersion @@ -461,8 +473,10 @@ function Get-ChocoVersion ($action) { $res = [System.Version]($cmd.Split('-')[0]) $res | Export-Clixml -Path $chocoVersion } + } else { + Remove-Item $chocoVersion -Force + $res = $true } - Return $res } From 23b2ad871f0fcf420542e5b0e5b2f36d35e8e0f7 Mon Sep 17 00:00:00 2001 From: jrdnr Date: Thu, 21 Mar 2019 22:16:05 -0400 Subject: [PATCH 8/9] Revered if Purge.IsPresent checks back to origional order --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 79c2000..11a425e 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -433,7 +433,10 @@ function Get-ChocoInstalledPackage { } $ChocoInstallList = Join-Path -Path $ChocoInstallLP -ChildPath 'ChocoInstalled.xml' - if ( -not $Purge) { + if ($Purge) { + Remove-Item $ChocoInstallList -Force + $res = $true + } else { $PackageCacheSec = (Get-Date).AddSeconds('-60') if ( $PackageCacheSec -lt (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime ) { $res = Import-Clixml $ChocoInstallList @@ -443,9 +446,6 @@ function Get-ChocoInstalledPackage { $res | Export-Clixml -Path $ChocoInstallList } } - } else { - Remove-Item $ChocoInstallList -Force - $res = $true } Return $res @@ -465,6 +465,9 @@ function Get-ChocoVersion { $chocoVersion = Join-Path -Path $chocoInstallCache -ChildPath 'ChocoVersion.xml' if ( -not $Purge) { + Remove-Item $chocoVersion -Force + $res = $true + } else { $cacheSec = (Get-Date).AddSeconds('-60') if ( $cacheSec -lt (Get-Item $chocoVersion -ErrorAction SilentlyContinue).LastWriteTime ) { $res = Import-Clixml $chocoVersion @@ -473,9 +476,6 @@ function Get-ChocoVersion { $res = [System.Version]($cmd.Split('-')[0]) $res | Export-Clixml -Path $chocoVersion } - } else { - Remove-Item $chocoVersion -Force - $res = $true } Return $res } From 548cd0c9638a20a57c7184f801d716a6ed6ccfe9 Mon Sep 17 00:00:00 2001 From: jrdnr Date: Fri, 22 Mar 2019 08:25:35 -0400 Subject: [PATCH 9/9] Corrected reversed logic in `if (-not $Purge) Using `$Purge.IsPresent` to be more explicit. --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 11a425e..5221c08 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -433,7 +433,7 @@ function Get-ChocoInstalledPackage { } $ChocoInstallList = Join-Path -Path $ChocoInstallLP -ChildPath 'ChocoInstalled.xml' - if ($Purge) { + if ($Purge.IsPresent) { Remove-Item $ChocoInstallList -Force $res = $true } else { @@ -464,7 +464,7 @@ function Get-ChocoVersion { } $chocoVersion = Join-Path -Path $chocoInstallCache -ChildPath 'ChocoVersion.xml' - if ( -not $Purge) { + if ($Purge.IsPresent) { Remove-Item $chocoVersion -Force $res = $true } else {