diff --git a/nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1 b/nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1 index 76ad358090..9357467eb3 100644 --- a/nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1 +++ b/nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1 @@ -858,7 +858,7 @@ function Invoke-Chocolatey-Initial { try { $chocoInstallationFolder = Get-ChocolateyInstallFolder $chocoExe = Join-Path -Path $chocoInstallationFolder -ChildPath "choco.exe" - $runResult = & $chocoExe -v *>&1 + $runResult = & $chocoExe -v 2>&1 if ($LASTEXITCODE -eq 0) { Write-Debug "Chocolatey CLI execution completed successfully." } diff --git a/src/chocolatey.resources/helpers/chocolateyInstaller.psm1 b/src/chocolatey.resources/helpers/chocolateyInstaller.psm1 index dfa78d18c6..86e3d52e08 100644 --- a/src/chocolatey.resources/helpers/chocolateyInstaller.psm1 +++ b/src/chocolatey.resources/helpers/chocolateyInstaller.psm1 @@ -153,26 +153,31 @@ if (Test-Path $extensionsPath) { # In effect we ensure that any command calls that match the name of one of our commands # will resolve to _our_ commands (preferring licensed cmdlets in the case of a name collision), # preventing packages from overriding them with their own commands and potentially breaking things. -$ExecutionContext.InvokeCommand.PreCommandLookupAction = { - param($command, $eventArgs) +# +# This functionality is only available in v3 and later, so using this in v2 will not +# work; check for the property before trying to set it. +if ($ExecutionContext.InvokeCommand.PreCommandLookupAction) { + $ExecutionContext.InvokeCommand.PreCommandLookupAction = { + param($command, $eventArgs) - # Don't run this handler for stuff PowerShell is looking up internally - if ($eventArgs.CommandOrigin -eq 'Runspace') { - $resolvedCommand = if ($command -in $chocolateyCmdlets.Licensed) { - Get-Command "$command*" -Module 'chocolatey.licensed' -CommandType Cmdlet -ErrorAction Ignore | - Where-Object { $_.Name -match "^$command(Cmdlet)?$" } | - Select-Object -First 1 - } - elseif ($command -in $chocolateyCmdlets.Default) { - Get-Command $command -Module "Chocolatey.PowerShell" -CommandType Cmdlet -ErrorAction Ignore - } + # Don't run this handler for stuff PowerShell is looking up internally + if ($eventArgs.CommandOrigin -eq 'Runspace') { + $resolvedCommand = if ($chocolateyCmdlets.Licensed -contains $command) { + Get-Command "$command*" -Module 'chocolatey.licensed' -CommandType Cmdlet -ErrorAction SilentlyContinue | + Where-Object { @($command, "$($command)Cmdlet") -contains $_.Name } | + Select-Object -First 1 + } + elseif ($chocolateyCmdlets.Default -contains $command) { + Get-Command $command -Module "Chocolatey.PowerShell" -CommandType Cmdlet -ErrorAction SilentlyContinue + } - if ($resolvedCommand) { - $eventArgs.Command = $resolvedCommand - $eventArgs.StopSearch = $true + if ($resolvedCommand) { + $eventArgs.Command = $resolvedCommand + $eventArgs.StopSearch = $true + } } - } -}.GetNewClosure() + }.GetNewClosure() +} # todo: explore removing this for a future version Export-ModuleMember -Function * -Alias * -Cmdlet *