From 13dc62bfe7cb900a2f7e8be72e721b14da45264f Mon Sep 17 00:00:00 2001 From: edwin Date: Mon, 4 Dec 2017 13:56:15 +0100 Subject: [PATCH 01/22] add Invoke-Chocolatey cmdlet which throws error on choco failure --- .../cChocoPackageInstall.psm1 | 65 +++++++++++++++++-- Tests/cChocoPackageInstall_Tests.ps1 | 12 ++++ 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 91acfae..0bd7104 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -240,8 +240,9 @@ function InstallPackage $chocoinstallparams += " $cParams" } Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'" - - $packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams" + $procInfo = new-object System.Diagnostics.ProcessStartInfo + $procInfo.filenameklkllk + $packageInstallOuput = Invoke-ChocoLatey "install $pName $chocoinstallparams" Write-Verbose -Message "Package output $packageInstallOuput " #refresh path varaible in powershell, as choco doesn"t, to pull in git @@ -263,12 +264,12 @@ function UninstallPackage if (-not ($pParams)) { Write-Verbose -Message 'Uninstalling Package Standard' - $packageUninstallOuput = choco uninstall $pName -y + $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName -y" } elseif ($pParams) { Write-Verbose -Message "Uninstalling Package with params $pParams" - $packageUninstallOuput = choco uninstall $pName --params="$pParams" -y + $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName --params=`"$pParams`" -y" } Write-Verbose -Message "Package uninstall output $packageUninstallOuput " @@ -326,7 +327,7 @@ Function Test-LatestVersionInstalled { Write-Verbose -Message "Testing if $pName can be upgraded: 'choco upgrade $pName $chocoupgradeparams'" - $packageUpgradeOuput = Invoke-Expression -Command "choco upgrade $pName $chocoupgradeparams" + $packageUpgradeOuput = Invoke-Chocolatey "upgrade $pName $chocoupgradeparams" $packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_} if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") { @@ -383,7 +384,7 @@ Function Upgrade-Package { if ($cParams) { $chocoupgradeparams += " $cParams" } - $cmd = "choco upgrade $pName $chocoupgradeparams" + $cmd = "upgrade $pName $chocoupgradeparams" Write-Verbose -Message "Upgrade command: '$cmd'" if (-not (IsPackageInstalled -pName $pName)) @@ -391,7 +392,7 @@ Function Upgrade-Package { throw "$pName is not installed, you cannot upgrade" } - $packageUpgradeOuput = Invoke-Expression -Command $cmd + $packageUpgradeOuput = Invoke-Chocolatey $cmd $packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ } } @@ -399,4 +400,54 @@ function Get-ChocoInstalledPackage { Return (choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|") } + +<# +.Synopsis + Invoke the chocolatey executable on the sytem. Throws choco error +.DESCRIPTION + Invoke the chocolatey executable on the sytem. Throws choco error, also when used in DSC configurations. +.EXAMPLE + Invoke-Chocolatey "list" +.EXAMPLE + Invoke-Chocolatey "list -lo" +.EXAMPLE + IChoco "list -lo" +#> +function Invoke-Chocolatey +{ + [CmdletBinding()] + [Alias("IChoco")] + [OutputType([int])] + Param + ( + # Param1 help description + [Parameter(Position=0)] + $pParams + ) + Process + { + Write-Verbose -Message "command: 'choco $pParams'" + $pinfo = New-Object System.Diagnostics.ProcessStartInfo + $pinfo.FileName = "choco" + $pinfo.RedirectStandardError = $true + $pinfo.RedirectStandardOutput = $true + $pinfo.UseShellExecute = $false + $pinfo.Arguments = $pParams + $p = New-Object System.Diagnostics.Process + $p.StartInfo = $pinfo + $p.Start() | Out-Null + $output = $p.StandardOutput.ReadToEnd() + $p.WaitForExit() + + if($p.ExitCode -eq 0) + { + $output + } + else + { + throw $output + } + } +} + Export-ModuleMember -Function *-TargetResource diff --git a/Tests/cChocoPackageInstall_Tests.ps1 b/Tests/cChocoPackageInstall_Tests.ps1 index e522f64..349f248 100644 --- a/Tests/cChocoPackageInstall_Tests.ps1 +++ b/Tests/cChocoPackageInstall_Tests.ps1 @@ -122,6 +122,18 @@ Describe -Name "Testing $ResourceName loaded from $ResourceFile" -Fixture { Test-TargetResource @Scenario4 | Should Be $False } } + + Context -Name "Package cannot be found" -Fixture { + + $Scenario1 = @{ + Name = 'NonExistendPackage' + Ensure = 'Present' + } + It -name "Set-TargeResource -ensure 'present' should throw" -test { + { Set-TargetResource @Scenario1 } | Should throw + } + + } } #Clean-up From e3d87fbf6a9135920fc1a228707080e7b945bfd3 Mon Sep 17 00:00:00 2001 From: edwin Date: Mon, 4 Dec 2017 14:15:27 +0100 Subject: [PATCH 02/22] removed typo --- .../cChocoPackageInstall.psm1 | 35 ++----------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index dc0e706..c531f24 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Chocolatey Software, Inc. +# Copyright (c) 2017 Chocolatey Software, Inc. # Copyright (c) 2013 - 2017 Lawrence Gripper & original authors/contributors from https://github.com/chocolatey/cChoco # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -240,14 +240,9 @@ function InstallPackage $chocoinstallparams += " $cParams" } Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'" - $procInfo = new-object System.Diagnostics.ProcessStartInfo - $procInfo.filenameklkllk $packageInstallOuput = Invoke-ChocoLatey "install $pName $chocoinstallparams" Write-Verbose -Message "Package output $packageInstallOuput " - # Clear Package Cache - Get-ChocoInstalledPackage 'Purge' - #refresh path varaible in powershell, as choco doesn"t, to pull in git $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') } @@ -277,9 +272,6 @@ function UninstallPackage Write-Verbose -Message "Package uninstall output $packageUninstallOuput " - # Clear Package Cache - Get-ChocoInstalledPackage 'Purge' - #refresh path varaible in powershell, as choco doesn"t, to pull in git $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') } @@ -400,31 +392,10 @@ Function Upgrade-Package { $packageUpgradeOuput = Invoke-Chocolatey $cmd $packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ } - - # Clear Package Cache - Get-ChocoInstalledPackage 'Purge' } -function Get-ChocoInstalledPackage ($action) { - $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 { - $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 - } - } - - Return $res +function Get-ChocoInstalledPackage { + Return (choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|") } From b5e589ec1702b82a7e0009017f5d5cd5e4d488cd Mon Sep 17 00:00:00 2001 From: edwin Date: Wed, 13 Dec 2017 22:38:02 +0100 Subject: [PATCH 03/22] removed alias, ensured LASTEXITCODE powershell is set same as choco exitcode. Some casting --- .../cChocoPackageInstall.psm1 | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index c531f24..a38aeb1 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -91,26 +91,26 @@ function Set-TargetResource $whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Remove Chocolatey package') if ($whatIfShouldProcess) { Write-Verbose -Message "Removing $Name as ensure is set to absent" - UninstallPackage -pName $Name -pParams $Params + UninstallPackage -pName $Name -arguments $Params } } else { $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 + UninstallPackage -pName $Name -arguments $Params Write-Verbose -Message "Re-Installing $Name with correct version $version" - InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams + InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams } elseif ($AutoUpgrade) { Write-Verbose -Message "Upgrading $Name due to version mis-match" - Upgrade-Package -pName $Name -pParams $Params -pSource $Source + Upgrade-Package -pName $Name -arguments $Params -pSource $Source } } } } else { $whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Install package from Chocolatey') if ($whatIfShouldProcess) { - InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams + InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams } } } @@ -215,7 +215,7 @@ function InstallPackage [Parameter(Position=0,Mandatory)] [string]$pName, [Parameter(Position=1)] - [string]$pParams, + [string]$arguments, [Parameter(Position=2)] [string]$pVersion, [Parameter(Position=3)] @@ -227,8 +227,8 @@ function InstallPackage $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') [string]$chocoinstallparams = '-y' - if ($pParams) { - $chocoinstallparams += " --params=`"$pParams`"" + if ($arguments) { + $chocoinstallparams += " --params=`"$arguments`"" } if ($pVersion) { $chocoinstallparams += " --version=`"$pVersion`"" @@ -253,21 +253,21 @@ function UninstallPackage [Parameter(Position=0,Mandatory)] [string]$pName, [Parameter(Position=1)] - [string]$pParams + [string]$arguments ) $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') #Todo: Refactor - if (-not ($pParams)) + if (-not ($arguments)) { Write-Verbose -Message 'Uninstalling Package Standard' $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName -y" } - elseif ($pParams) + elseif ($arguments) { - Write-Verbose -Message "Uninstalling Package with params $pParams" - $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName --params=`"$pParams`" -y" + Write-Verbose -Message "Uninstalling Package with params $arguments" + $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName --params=`"$arguments`" -y" } Write-Verbose -Message "Package uninstall output $packageUninstallOuput " @@ -362,7 +362,7 @@ Function Upgrade-Package { [Parameter(Position=0,Mandatory)] [string]$pName, [Parameter(Position=1)] - [string]$pParams, + [string]$arguments, [Parameter(Position=2)] [string]$pSource, [Parameter(Position=3)] @@ -373,8 +373,8 @@ Function Upgrade-Package { Write-Verbose -Message "Path variables: $env:Path" [string]$chocoupgradeparams = '-dv -y' - if ($pParams) { - $chocoupgradeparams += " --params=`"$pParams`"" + if ($arguments) { + $chocoupgradeparams += " --params=`"$arguments`"" } if ($pSource) { $chocoupgradeparams += " --source=`"$pSource`"" @@ -414,36 +414,38 @@ function Get-ChocoInstalledPackage { function Invoke-Chocolatey { [CmdletBinding()] - [Alias("IChoco")] - [OutputType([int])] + Param ( # Param1 help description [Parameter(Position=0)] - $pParams + $arguments ) Process { - Write-Verbose -Message "command: 'choco $pParams'" + Write-Verbose -Message "command: 'choco $arguments'" $pinfo = New-Object System.Diagnostics.ProcessStartInfo $pinfo.FileName = "choco" $pinfo.RedirectStandardError = $true $pinfo.RedirectStandardOutput = $true $pinfo.UseShellExecute = $false - $pinfo.Arguments = $pParams + $pinfo.Arguments = $arguments $p = New-Object System.Diagnostics.Process $p.StartInfo = $pinfo $p.Start() | Out-Null $output = $p.StandardOutput.ReadToEnd() $p.WaitForExit() - + #cast output to object array + [object[]]$result = $output.Split("`n") if($p.ExitCode -eq 0) { - $output + $result } else { - throw $output + #ensure $LASTEXITCODE equals exit code choco. + powershell "exit $($p.ExitCode)" + Write-Error $output -ErrorAction Stop } } } From f8ad82e32c0f4fe7c507e9cec16f265c97a0e28b Mon Sep 17 00:00:00 2001 From: edwin Date: Fri, 15 Dec 2017 12:52:10 +0100 Subject: [PATCH 04/22] add lastexitcode after invoke-chocolatey, included valid exit codes --- .../cChocoPackageInstall.psm1 | 99 ++++++++++--------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index a38aeb1..413085a 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -215,7 +215,7 @@ function InstallPackage [Parameter(Position=0,Mandatory)] [string]$pName, [Parameter(Position=1)] - [string]$arguments, + [string]$pParams, [Parameter(Position=2)] [string]$pVersion, [Parameter(Position=3)] @@ -227,8 +227,8 @@ function InstallPackage $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') [string]$chocoinstallparams = '-y' - if ($arguments) { - $chocoinstallparams += " --params=`"$arguments`"" + if ($pParams) { + $chocoinstallparams += " --params=`"$pParams`"" } if ($pVersion) { $chocoinstallparams += " --version=`"$pVersion`"" @@ -253,21 +253,21 @@ function UninstallPackage [Parameter(Position=0,Mandatory)] [string]$pName, [Parameter(Position=1)] - [string]$arguments + [string]$pParams ) $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') #Todo: Refactor - if (-not ($arguments)) + if (-not ($pParams)) { Write-Verbose -Message 'Uninstalling Package Standard' $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName -y" } - elseif ($arguments) + elseif ($pParams) { - Write-Verbose -Message "Uninstalling Package with params $arguments" - $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName --params=`"$arguments`" -y" + Write-Verbose -Message "Uninstalling Package with params $pParams" + $packageUninstallOuput = Invoke-Chocolatey "uninstall $pName --params=`"$pParams`" -y" } Write-Verbose -Message "Package uninstall output $packageUninstallOuput " @@ -362,7 +362,7 @@ Function Upgrade-Package { [Parameter(Position=0,Mandatory)] [string]$pName, [Parameter(Position=1)] - [string]$arguments, + [string]$pParams, [Parameter(Position=2)] [string]$pSource, [Parameter(Position=3)] @@ -373,8 +373,8 @@ Function Upgrade-Package { Write-Verbose -Message "Path variables: $env:Path" [string]$chocoupgradeparams = '-dv -y' - if ($arguments) { - $chocoupgradeparams += " --params=`"$arguments`"" + if ($pParams) { + $chocoupgradeparams += " --params=`"$pParams`"" } if ($pSource) { $chocoupgradeparams += " --source=`"$pSource`"" @@ -401,53 +401,62 @@ function Get-ChocoInstalledPackage { <# .Synopsis - Invoke the chocolatey executable on the sytem. Throws choco error + Run chocolatey executable and throws error on failure .DESCRIPTION - Invoke the chocolatey executable on the sytem. Throws choco error, also when used in DSC configurations. -.EXAMPLE - Invoke-Chocolatey "list" + Run chocolatey executable and throws error on failure .EXAMPLE - Invoke-Chocolatey "list -lo" + Invoke-Chocolatey "list -lo" .EXAMPLE - IChoco "list -lo" + Invoke-Chocolatey -arguments "list -lo" #> function Invoke-Chocolatey { [CmdletBinding()] - Param ( - # Param1 help description + # chocolatey arguments." [Parameter(Position=0)] - $arguments + [string]$arguments ) - Process + + [int[]]$validExitCodes = $( + 0, #most widely used success exit code + 1605, #(MSI uninstall) - the product is not found, could have already been uninstalled + 1614, #(MSI uninstall) - the product is uninstalled + 1641, #(MSI) - restart initiated + 3010 #(MSI, InnoSetup can be passed to provide this) - restart required + ) + Write-Verbose -Message "command: 'choco $arguments'" + + $pinfo = New-Object System.Diagnostics.ProcessStartInfo + $pinfo.FileName = "choco" + $pinfo.RedirectStandardError = $true + $pinfo.RedirectStandardOutput = $true + $pinfo.UseShellExecute = $false + $pinfo.Arguments = "$arguments" + + $p = New-Object System.Diagnostics.Process + $p.StartInfo = $pinfo + $p.Start() | Out-Null + + $output = $p.StandardOutput.ReadToEnd() + $errorMsg = $p.StandardError.ReadToEnd() + $p.WaitForExit() + $exitcode = $p.ExitCode + $p.Dispose() + + #Set $LASTEXITCODE variable. + powershell.exe -NoLogo -NoProfile -Noninteractive "exit $exitcode" + + if($exitcode -in $validExitCodes ) { - Write-Verbose -Message "command: 'choco $arguments'" - $pinfo = New-Object System.Diagnostics.ProcessStartInfo - $pinfo.FileName = "choco" - $pinfo.RedirectStandardError = $true - $pinfo.RedirectStandardOutput = $true - $pinfo.UseShellExecute = $false - $pinfo.Arguments = $arguments - $p = New-Object System.Diagnostics.Process - $p.StartInfo = $pinfo - $p.Start() | Out-Null - $output = $p.StandardOutput.ReadToEnd() - $p.WaitForExit() - #cast output to object array - [object[]]$result = $output.Split("`n") - if($p.ExitCode -eq 0) - { - $result - } - else - { - #ensure $LASTEXITCODE equals exit code choco. - powershell "exit $($p.ExitCode)" - Write-Error $output -ErrorAction Stop - } + [object[]]$output.Split("`n") } + else + { + #when error, throw output as error, contains errormessage + throw "Error: chocolatey command failed with exit code $exitcode.`n$output" + } } Export-ModuleMember -Function *-TargetResource From cfb4fad73446284600a7a083f47f040591badb6c Mon Sep 17 00:00:00 2001 From: edwin Date: Fri, 15 Dec 2017 13:11:14 +0100 Subject: [PATCH 05/22] some fixes --- 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 413085a..a46e6d2 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -419,7 +419,7 @@ function Invoke-Chocolatey [string]$arguments ) - [int[]]$validExitCodes = $( + $validExitCodes = $( 0, #most widely used success exit code 1605, #(MSI uninstall) - the product is not found, could have already been uninstalled 1614, #(MSI uninstall) - the product is uninstalled @@ -450,7 +450,7 @@ function Invoke-Chocolatey if($exitcode -in $validExitCodes ) { - [object[]]$output.Split("`n") + $output.Split("`n") } else { From db65157247630f2a340ba96bcbdf731598391781 Mon Sep 17 00:00:00 2001 From: edwin Date: Fri, 15 Dec 2017 13:26:53 +0100 Subject: [PATCH 06/22] removed unused variable --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 1 - 1 file changed, 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index a46e6d2..ec116e0 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -440,7 +440,6 @@ function Invoke-Chocolatey $p.Start() | Out-Null $output = $p.StandardOutput.ReadToEnd() - $errorMsg = $p.StandardError.ReadToEnd() $p.WaitForExit() $exitcode = $p.ExitCode $p.Dispose() From edd29ac4fe6fd6cf2b250524f84739d2acb73d4b Mon Sep 17 00:00:00 2001 From: edwin Date: Fri, 15 Dec 2017 13:43:41 +0100 Subject: [PATCH 07/22] invoke-chocolatey now returns object array (same as chocolatey --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index ec116e0..f7fb6d4 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -338,6 +338,7 @@ Function Test-LatestVersionInstalled { ##attempting to work around the issues with Chocolatey calling Write-host in its scripts. function global:Write-Host { + [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidGlobalFunctions','')] Param( [Parameter(Mandatory, Position = 0)] [Object] @@ -419,7 +420,7 @@ function Invoke-Chocolatey [string]$arguments ) - $validExitCodes = $( + [int[]]$validExitCodes = $( 0, #most widely used success exit code 1605, #(MSI uninstall) - the product is not found, could have already been uninstalled 1614, #(MSI uninstall) - the product is uninstalled @@ -449,7 +450,7 @@ function Invoke-Chocolatey if($exitcode -in $validExitCodes ) { - $output.Split("`n") + [object[]]$outputdata = ($output.Split("`n") | ForEach-Object { ConvertFrom-String -InputObject $_ }) } else { From 2285eed4362b98f2edbb29ba915449ef14554c27 Mon Sep 17 00:00:00 2001 From: edwin Date: Fri, 15 Dec 2017 13:47:12 +0100 Subject: [PATCH 08/22] inovke chocolatey now returning output --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index f7fb6d4..bba9de3 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -451,6 +451,7 @@ function Invoke-Chocolatey if($exitcode -in $validExitCodes ) { [object[]]$outputdata = ($output.Split("`n") | ForEach-Object { ConvertFrom-String -InputObject $_ }) + $outputdata } else { From 60b5c52b247ceacd8dc0095fb9543a916ba3d06e Mon Sep 17 00:00:00 2001 From: edwin Date: Fri, 15 Dec 2017 14:07:08 +0100 Subject: [PATCH 09/22] change to output --- 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 bba9de3..5dd6a9e 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -450,7 +450,7 @@ function Invoke-Chocolatey if($exitcode -in $validExitCodes ) { - [object[]]$outputdata = ($output.Split("`n") | ForEach-Object { ConvertFrom-String -InputObject $_ }) + $output.Split("`n") $outputdata } else From 119a7e38e8ee0c9e120d343e5a1342ba82fa1e4f Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 7 Oct 2019 09:36:00 +0200 Subject: [PATCH 10/22] some minor symantic changes --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 5dd6a9e..3f2d557 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -1,5 +1,4 @@ -# Copyright (c) 2017 Chocolatey Software, Inc. -# Copyright (c) 2013 - 2017 Lawrence Gripper & original authors/contributors from https://github.com/chocolatey/cChoco +# Copyright (c) 2013 - 2017 Lawrence Gripper & original authors/contributors from https://github.com/chocolatey/cChoco # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -240,7 +239,7 @@ function InstallPackage $chocoinstallparams += " $cParams" } Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'" - $packageInstallOuput = Invoke-ChocoLatey "install $pName $chocoinstallparams" + $packageInstallOuput = Invoke-Chocolatey "install $pName $chocoinstallparams" Write-Verbose -Message "Package output $packageInstallOuput " #refresh path varaible in powershell, as choco doesn"t, to pull in git @@ -325,7 +324,7 @@ Function Test-LatestVersionInstalled { Write-Verbose -Message "Testing if $pName can be upgraded: 'choco upgrade $pName $chocoupgradeparams'" - $packageUpgradeOuput = Invoke-Chocolatey "upgrade $pName $chocoupgradeparams" + $packageUpgradeOuput = Invoke-Chocolatey "choco upgrade $pName $chocoupgradeparams" $packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_} if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") { @@ -396,7 +395,7 @@ Function Upgrade-Package { } function Get-ChocoInstalledPackage { - Return (choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|") + (choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|") } From 1b84d8c1489ca2067321bc5c53a191f1e147a40b Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 14 Oct 2019 10:02:42 +0200 Subject: [PATCH 11/22] reverted some changes --- .../cChocoPackageInstall/cChocoPackageInstall.psm1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 3f2d557..dfb3293 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -242,6 +242,10 @@ function InstallPackage $packageInstallOuput = Invoke-Chocolatey "install $pName $chocoinstallparams" Write-Verbose -Message "Package output $packageInstallOuput " + + # Clear Package Cache + Get-ChocoInstalledPackage 'Purge' + #refresh path varaible in powershell, as choco doesn"t, to pull in git $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') } @@ -271,6 +275,9 @@ function UninstallPackage Write-Verbose -Message "Package uninstall output $packageUninstallOuput " + # Clear Package Cache + Get-ChocoInstalledPackage 'Purge' + #refresh path varaible in powershell, as choco doesn"t, to pull in git $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') } @@ -324,7 +331,7 @@ Function Test-LatestVersionInstalled { Write-Verbose -Message "Testing if $pName can be upgraded: 'choco upgrade $pName $chocoupgradeparams'" - $packageUpgradeOuput = Invoke-Chocolatey "choco upgrade $pName $chocoupgradeparams" + $packageUpgradeOuput = Invoke-Chocolatey "upgrade $pName $chocoupgradeparams" $packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_} if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") { From 36cbc0fda21854ac3e49f3466c5b1be8bc16aa0d Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 14:53:37 +0200 Subject: [PATCH 12/22] typo --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 97586a7..e541eed 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -101,7 +101,9 @@ function Set-TargetResource InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams } elseif ($AutoUpgrade) { Write-Verbose -Message "Upgrading $Name due to version mis-match" - InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams + InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams + } + } } } From a31199b91399d2d63f873340e0f1e4f38cc83a17 Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 14:57:39 +0200 Subject: [PATCH 13/22] missed some bracket --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index e541eed..770618e 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -104,6 +104,7 @@ function Set-TargetResource InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams } } + } } } @@ -172,6 +173,7 @@ function Test-TargetResource Return $result } + function Test-ChocoInstalled { Write-Verbose -Message 'Test-ChocoInstalled' @@ -407,9 +409,6 @@ function Invoke-Chocolatey } } - - - function Get-ChocoVersion { [CmdletBinding()] param ( From f918292ee6e77bfb570f5f7d85d72e053df7881b Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 15:15:49 +0200 Subject: [PATCH 14/22] add missing function --- .../cChocoPackageInstall.psm1 | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 770618e..65c981e 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -274,6 +274,101 @@ function global:Write-Host #Override default Write-Host... Write-Verbose -Message $Object } +function UninstallPackage +{ + [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] + param( + [Parameter(Position=0,Mandatory)] + [string]$pName, + [Parameter(Position=1)] + [string]$pParams + ) + + $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + + [string]$chocoParams = "-y" + if ($pParams) { + $chocoParams += " --params=`"$pParams`"" + } + if ($pVersion) { + $chocoParams += " --version=`"$pVersion`"" + } + # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress + if ((Get-ChocoVersion) -ge [System.Version]('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 " + + # Clear Package Cache + Get-ChocoInstalledPackage -Purge + + #refresh path varaible in powershell, as choco doesn"t, to pull in git + $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') +} + +function IsPackageInstalled +{ + param( + [Parameter(Position=0,Mandatory)][string]$pName, + [Parameter(Position=1)][string]$pVersion + ) + Write-Verbose -Message "Start IsPackageInstalled $pName" + + $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + Write-Verbose -Message "Path variables: $env:Path" + + $installedPackages = Get-ChocoInstalledPackage + + if ($pVersion) { + Write-Verbose 'Comparing version' + $installedPackages = $installedPackages | Where-object { $_.Name -eq $pName -and $_.Version -eq $pVersion} + } else { + Write-Verbose "Finding packages -eq $pName" + $installedPackages = $installedPackages | Where-object { $_.Name -eq $pName} + } + + $count = @($installedPackages).Count + Write-Verbose "Found $Count matching packages" + if ($Count -gt 0) + { + $installedPackages | ForEach-Object {Write-Verbose -Message "Found: $($_.Name) with version $($_.Version)"} + return $true + } + + return $false +} + +Function Test-LatestVersionInstalled { + [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] + param( + [Parameter(Mandatory)] + [string]$pName, + [string]$pSource + ) + Write-Verbose -Message "Testing if $pName can be upgraded" + + [string]$chocoParams = '--noop' + if ($pSource) { + $chocoParams += " --source=`"$pSource`"" + } + + $cmd = "upgrade $pName $chocoParams" + Write-Verbose -Message "Testing if $pName can be upgraded: choco '$cmd'" + + $packageUpgradeOuput = Invoke-ChocoLatey $cmd + $packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_} + + if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") { + return $true + } + return $false +} + Function Upgrade-Package { [Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs','')] From a765b4ace8d9d4406cec3ebbce68f6e4c1c9f5dd Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 16:09:00 +0200 Subject: [PATCH 15/22] else block added, gone after merge --- .../cChocoPackageInstall.psm1 | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 65c981e..1381039 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -105,6 +105,11 @@ function Set-TargetResource } } } + }else{ + $whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Install package from Chocolatey') + if ($whatIfShouldProcess) { + InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams + } } } @@ -465,11 +470,11 @@ function Invoke-Chocolatey [string]$arguments ) [int[]]$validExitCodes = $( - 0, #most widely used success exit code - 1605, #(MSI uninstall) - the product is not found, could have already been uninstalled - 1614, #(MSI uninstall) - the product is uninstalled - 1641, #(MSI) - restart initiated - 3010 #(MSI, InnoSetup can be passed to provide this) - restart required + 0 #most widely used success exit code + #1605, #(MSI uninstall) - the product is not found, could have already been uninstalled + #1614, #(MSI uninstall) - the product is uninstalled + #1641, #(MSI) - restart initiated + #3010 #(MSI, InnoSetup can be passed to provide this) - restart required ) Write-Verbose -Message "command: 'choco $arguments'" From c62705e629cb7eda494941717a2ae313f133d0cc Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 17:25:39 +0200 Subject: [PATCH 16/22] should process with test not used --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 1 - 1 file changed, 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 1381039..557c68e 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -115,7 +115,6 @@ function Set-TargetResource function Test-TargetResource { - [CmdletBinding(SupportsShouldProcess)] [OutputType([bool])] param ( From 6f99ed13b123bcdc7d78ccf0e8e0a86562c3d629 Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 17:38:30 +0200 Subject: [PATCH 17/22] add shouldprocess to Invoke chocolatey --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 557c68e..5d369f9 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -115,6 +115,7 @@ function Set-TargetResource function Test-TargetResource { + [CmdletBinding(SupportsShouldProcess)] [OutputType([bool])] param ( @@ -461,7 +462,7 @@ function Get-ChocoInstalledPackage { #> function Invoke-Chocolatey { - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess)] Param ( # chocolatey arguments." From ff013baf397837a01548f69723b77d1b375a7839 Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 17:52:08 +0200 Subject: [PATCH 18/22] add verbose to scriptstest --- Tests/cChoco_ScriptAnalyzerTests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/cChoco_ScriptAnalyzerTests.ps1 b/Tests/cChoco_ScriptAnalyzerTests.ps1 index 699758b..ff85b63 100644 --- a/Tests/cChoco_ScriptAnalyzerTests.ps1 +++ b/Tests/cChoco_ScriptAnalyzerTests.ps1 @@ -29,6 +29,7 @@ if ($Modules.count -gt 0) { foreach ($module in $modules) { Context “Testing Module '$($module.FullName)'” { foreach ($rule in $rules) { + write-verbose (Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName | fl) It “passes the PSScriptAnalyzer Rule $rule“ { (Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0 } From 8d1c67fad630c1e5ef44a0ca41f10c733f878e23 Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 18:01:25 +0200 Subject: [PATCH 19/22] shouldprocess thing error --- DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 | 2 +- Tests/cChoco_ScriptAnalyzerTests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 5d369f9..6e3d422 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -510,7 +510,7 @@ function Invoke-Chocolatey } function Get-ChocoVersion { - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess)] param ( [switch]$Purge, [switch]$NoCache diff --git a/Tests/cChoco_ScriptAnalyzerTests.ps1 b/Tests/cChoco_ScriptAnalyzerTests.ps1 index ff85b63..98c8e7b 100644 --- a/Tests/cChoco_ScriptAnalyzerTests.ps1 +++ b/Tests/cChoco_ScriptAnalyzerTests.ps1 @@ -29,7 +29,7 @@ if ($Modules.count -gt 0) { foreach ($module in $modules) { Context “Testing Module '$($module.FullName)'” { foreach ($rule in $rules) { - write-verbose (Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName | fl) + Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName | fl It “passes the PSScriptAnalyzer Rule $rule“ { (Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0 } From d6c73a35ae378c55c3a3e6672586ee68a114d933 Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 18:15:47 +0200 Subject: [PATCH 20/22] no error local found with shouldprocess --- 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 6e3d422..1c51824 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -115,7 +115,7 @@ function Set-TargetResource function Test-TargetResource { - [CmdletBinding(SupportsShouldProcess)] + [CmdletBinding()] [OutputType([bool])] param ( @@ -462,7 +462,7 @@ function Get-ChocoInstalledPackage { #> function Invoke-Chocolatey { - [CmdletBinding(SupportsShouldProcess)] + [CmdletBinding()] Param ( # chocolatey arguments." @@ -510,7 +510,7 @@ function Invoke-Chocolatey } function Get-ChocoVersion { - [CmdletBinding(SupportsShouldProcess)] + [CmdletBinding()] param ( [switch]$Purge, [switch]$NoCache From 7270d0ab215214cf79d84dfb9bc7ff20a5f1fb43 Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 18:21:41 +0200 Subject: [PATCH 21/22] mm --- 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 1c51824..6f90b2a 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -115,7 +115,7 @@ function Set-TargetResource function Test-TargetResource { - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess)] [OutputType([bool])] param ( @@ -197,7 +197,7 @@ function Test-ChocoInstalled Function Test-Command { - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess)] [OutputType([bool])] Param ( [string]$command = 'choco' From 93afb734119f4e5bf2245fe84aab4fad68e24b5c Mon Sep 17 00:00:00 2001 From: edwin siebes Date: Mon, 25 May 2020 14:53:37 +0200 Subject: [PATCH 22/22] Created function invoke-chocolatey to ensure powershell exitcode equals choco exitcode typo missed some bracket add missing function else block added, gone after merge should process with test not used add shouldprocess to Invoke chocolatey add verbose to scriptstest shouldprocess thing error no error local found with shouldprocess mm --- .../cChocoPackageInstall.psm1 | 121 ++++++++++++++++-- Tests/cChoco_ScriptAnalyzerTests.ps1 | 1 + 2 files changed, 112 insertions(+), 10 deletions(-) diff --git a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 index 97586a7..6f90b2a 100644 --- a/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 +++ b/DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1 @@ -101,7 +101,15 @@ function Set-TargetResource InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams } elseif ($AutoUpgrade) { Write-Verbose -Message "Upgrading $Name due to version mis-match" - InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams + InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams + } + } + } + }else{ + $whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Install package from Chocolatey') + if ($whatIfShouldProcess) { + InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams + } } } @@ -170,6 +178,7 @@ function Test-TargetResource Return $result } + function Test-ChocoInstalled { Write-Verbose -Message 'Test-ChocoInstalled' @@ -188,7 +197,7 @@ function Test-ChocoInstalled Function Test-Command { - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess)] [OutputType([bool])] Param ( [string]$command = 'choco' @@ -270,6 +279,101 @@ function global:Write-Host #Override default Write-Host... Write-Verbose -Message $Object } +function UninstallPackage +{ + [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] + param( + [Parameter(Position=0,Mandatory)] + [string]$pName, + [Parameter(Position=1)] + [string]$pParams + ) + + $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + + [string]$chocoParams = "-y" + if ($pParams) { + $chocoParams += " --params=`"$pParams`"" + } + if ($pVersion) { + $chocoParams += " --version=`"$pVersion`"" + } + # Check if Chocolatey version is Greater than 0.10.4, and add --no-progress + if ((Get-ChocoVersion) -ge [System.Version]('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 " + + # Clear Package Cache + Get-ChocoInstalledPackage -Purge + + #refresh path varaible in powershell, as choco doesn"t, to pull in git + $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') +} + +function IsPackageInstalled +{ + param( + [Parameter(Position=0,Mandatory)][string]$pName, + [Parameter(Position=1)][string]$pVersion + ) + Write-Verbose -Message "Start IsPackageInstalled $pName" + + $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + Write-Verbose -Message "Path variables: $env:Path" + + $installedPackages = Get-ChocoInstalledPackage + + if ($pVersion) { + Write-Verbose 'Comparing version' + $installedPackages = $installedPackages | Where-object { $_.Name -eq $pName -and $_.Version -eq $pVersion} + } else { + Write-Verbose "Finding packages -eq $pName" + $installedPackages = $installedPackages | Where-object { $_.Name -eq $pName} + } + + $count = @($installedPackages).Count + Write-Verbose "Found $Count matching packages" + if ($Count -gt 0) + { + $installedPackages | ForEach-Object {Write-Verbose -Message "Found: $($_.Name) with version $($_.Version)"} + return $true + } + + return $false +} + +Function Test-LatestVersionInstalled { + [Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')] + param( + [Parameter(Mandatory)] + [string]$pName, + [string]$pSource + ) + Write-Verbose -Message "Testing if $pName can be upgraded" + + [string]$chocoParams = '--noop' + if ($pSource) { + $chocoParams += " --source=`"$pSource`"" + } + + $cmd = "upgrade $pName $chocoParams" + Write-Verbose -Message "Testing if $pName can be upgraded: choco '$cmd'" + + $packageUpgradeOuput = Invoke-ChocoLatey $cmd + $packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_} + + if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") { + return $true + } + return $false +} + Function Upgrade-Package { [Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs','')] @@ -366,11 +470,11 @@ function Invoke-Chocolatey [string]$arguments ) [int[]]$validExitCodes = $( - 0, #most widely used success exit code - 1605, #(MSI uninstall) - the product is not found, could have already been uninstalled - 1614, #(MSI uninstall) - the product is uninstalled - 1641, #(MSI) - restart initiated - 3010 #(MSI, InnoSetup can be passed to provide this) - restart required + 0 #most widely used success exit code + #1605, #(MSI uninstall) - the product is not found, could have already been uninstalled + #1614, #(MSI uninstall) - the product is uninstalled + #1641, #(MSI) - restart initiated + #3010 #(MSI, InnoSetup can be passed to provide this) - restart required ) Write-Verbose -Message "command: 'choco $arguments'" @@ -405,9 +509,6 @@ function Invoke-Chocolatey } } - - - function Get-ChocoVersion { [CmdletBinding()] param ( diff --git a/Tests/cChoco_ScriptAnalyzerTests.ps1 b/Tests/cChoco_ScriptAnalyzerTests.ps1 index 699758b..98c8e7b 100644 --- a/Tests/cChoco_ScriptAnalyzerTests.ps1 +++ b/Tests/cChoco_ScriptAnalyzerTests.ps1 @@ -29,6 +29,7 @@ if ($Modules.count -gt 0) { foreach ($module in $modules) { Context “Testing Module '$($module.FullName)'” { foreach ($rule in $rules) { + Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName | fl It “passes the PSScriptAnalyzer Rule $rule“ { (Invoke-ScriptAnalyzer -Path $module.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0 }