Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot install Chocolatey - Invalid parameter Set-EnvironmentVariable : A parameter cannot be found that matches parameter name 'Scope' #3538

Closed
6 tasks done
pavelpikatamcs opened this issue Oct 28, 2024 · 3 comments

Comments

@pavelpikatamcs
Copy link

pavelpikatamcs commented Oct 28, 2024

Checklist

  • I confirm there are no unresolved issues reported on the Chocolatey Status page.
  • I have verified this is the correct repository for opening this issue.
  • I have verified no other issues exist related to my problem.
  • I have verified this is not an issue for a specific package.
  • I have verified this issue is not security related.
  • I confirm I am using official, and not unofficial, or modified, Chocolatey products.

What You Are Seeing?

Chocolatey's install script downloads Install-ChocolateyEnvironmentVariable.ps file which uses the following invalid code:
https://github.com/chocolatey/choco/blame/c8326fb81bb8f8bb741b3a18d2326d6f00bf43a4/src/chocolatey.resources/helpers/functions/Install-ChocolateyEnvironmentVariable.ps1#L117

if (Test-ProcessAdminRights) {
                # HKCU:\Environment may not exist, which happens sometimes with Server Core
                Set-EnvironmentVariable -Name $variableName -Value $variableValue -Scope Machine
            }

However, Set-EnvironmentVariable doesn't have Scope parameter.

What is Expected?

In order to set machine-level enviornment variable, the Target parameter must be used instead.
Sample: Set-EnvironmentVariable -Name $variableName -Value $variableValue -Target Machine
Docs from Get-Help:

Get-Help -Name Set-EnvironmentVariable

NAME
    Set-EnvironmentVariable
    
SYNTAX
    Set-EnvironmentVariable [-Name] <string> [-Value] <string> [-Target] {Process | User | Machine}  [<CommonParameters>]
    

ALIASES
    None
    

REMARKS
    None

How Did You Get This To Happen?

Execute install commands on Windows 11 host (23H2):

    Set-ExecutionPolicy Bypass -Scope Process -Force
    Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

System Details

  • Operating System: Windows 11 Enterprise multi-session 23H2
  • Windows PowerShell version: 5.1.22621.4249
  • Chocolatey CLI Version: 2.3.0
  • Chocolatey Licensed Extension version:
  • Chocolatey License type:
  • Terminal/Emulator: Powershell ISE

Installed Packages

None

Output Log

Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org)
Getting latest version of the Chocolatey package for download.
Not using proxy.
Getting Chocolatey from https://community.chocolatey.org/api/v2/package/chocolatey/2.3.0.
Downloading https://community.chocolatey.org/api/v2/package/chocolatey/2.3.0 to C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip
Not using proxy.
Extracting C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip to C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall
Remove-Item : Cannot remove item C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall\tools\chocolateyInstall\helpers\Chocolatey.PowerShell.dll: Access to the path 
'C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall\tools\chocolateyInstall\helpers\Chocolatey.PowerShell.dll' is denied.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:1029 char:29
+ ...             Remove-Item -LiteralPath $currentArchiveEntryFileInfo.Ful ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Users\devops....PowerShell.dll:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Installing Chocolatey on the local machine
Set-EnvironmentVariable : A parameter cannot be found that matches parameter name 'Scope'.
At C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall\tools\chocolateyInstall\helpers\functions\Install-ChocolateyEnvironmentVariable.ps1:117 char:83
+ ... mentVariable -Name $variableName -Value $variableValue -Scope Machine
+                                                            ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-EnvironmentVariable], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Set-EnvironmentVariable
 
Set-EnvironmentVariable : A parameter cannot be found that matches parameter name 'Scope'.
At C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall\tools\chocolateyInstall\helpers\functions\Install-ChocolateyEnvironmentVariable.ps1:103 char:79
+ ... mentVariable -Name $variableName -Value $variableValue -Scope $variab ...
+                                                            ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-EnvironmentVariable], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Set-EnvironmentVariable
 
Creating ChocolateyInstall as an environment variable (targeting 'Machine') 
  Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell 
  before you can use choco.
Set-EnvironmentVariable : A parameter cannot be found that matches parameter name 'Scope'.
At C:\Users\devops\AppData\Local\Temp\chocolatey\chocoInstall\tools\chocolateyInstall\helpers\functions\Install-ChocolateyEnvironmentVariable.ps1:103 char:79
+ ... mentVariable -Name $variableName -Value $variableValue -Scope $variab ...
+                                                            ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-EnvironmentVariable], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Set-EnvironmentVariable

Additional Context

No response

@pauby
Copy link
Member

pauby commented Oct 28, 2024

The Set-EnvironmentVariable cmdlet uses the -Scope parameter as you can see in the documentation.

I have just installed Chocolatey CLI 2.3.0 on a Windows 11 system (23H2 built 22631.3672) and it worked as expected.

Image

$PSVersionTable:

Name                           Value
----                           -----
PSVersion                      5.1.22621.3672
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.3672
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

I suspect the issue you are having is related to the earlier error. Are you installing within an elevated session? I also note you are running from PowerShell ISE which could be causing issues.

@pavelpikatamcs
Copy link
Author

pavelpikatamcs commented Oct 28, 2024

@pauby Ah, I thought Set-EnvironmentVariable function was part of some default PowerShell module.
Turns out, there is a conflict between Chocolatey's function, and a function from another module who also happen to have a function with the same name, but different signature.

Fixed by uninstalling module with conflicting function

@pauby
Copy link
Member

pauby commented Oct 29, 2024

Just following up here. Once #3477 is completed, the name collision should no longer be possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants