Skip to content

Commit

Permalink
If Chocolatey has just been installed yet its env variable is missing…
Browse files Browse the repository at this point in the history
…, force refresh PS and chocolatey environments
  • Loading branch information
kwein1 authored and gaelcolas committed Apr 2, 2021
1 parent 64f1aca commit 82c016b
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Chocolatey/DscResources/ChocolateyPackage/ChocolateyPackage.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ function Set-TargetResource
$ChocoCommand = switch ($Ensure) {
'Present' {
if ($testResult.PackagePresent -and !$testResult.VersionGreaterOrEqual) {
Get-Command Update-ChocolateyPackage
Get-Command Update-ChocolateyPackage
}
elseif (!$UpdateOnly) {
Get-Command Install-ChocolateyPackage
Get-Command Install-ChocolateyPackage
}
else {
Write-Verbose "Nothing to do: UpdateOnly : $UpdateOnly"
Expand Down Expand Up @@ -143,12 +143,12 @@ function Set-TargetResource
&$ChocoCommand @ChocoCommandParams -verbose | Write-Verbose

$PostActionResult = Test-ChocolateyPackageIsInstalled @TestParams
if ($PostActionResult.PackagePresent -and
if ($PostActionResult.PackagePresent -and
$PostActionResult.VersionGreaterOrEqual -and
$Ensure -eq 'Present') {
Write-Verbose -Message "--> Package Successfully Installed"
}
elseif ((!$PostActionResult.PackagePresent -or
elseif ((!$PostActionResult.PackagePresent -or
!$PostActionResult.VersionGreaterOrEqual) -and
$Ensure -eq 'Absent') {
Write-Verbose -Message "--> Package Successfully Removed"
Expand Down Expand Up @@ -206,6 +206,14 @@ function Test-TargetResource
}
}

Write-Verbose "Testing whether we need to refresh the PS environment so chocolatey doesn't fail"
if ($null -eq $env:ChocolateyInstall) {
write-verbose "Set ChocolateyInstall"
$env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
refreshenv > $null
}

$EnsureResultMap = @{
'Present' = $true
'Absent' = $false
Expand Down

0 comments on commit 82c016b

Please sign in to comment.