Coder Social home page Coder Social logo

Unable to install a package that requires PowerShell 6.2.0 min version as Choco uses an older PowerShell Version (5.x) about choco HOT 5 CLOSED

laura-rodriguez avatar laura-rodriguez commented on May 30, 2024
Unable to install a package that requires PowerShell 6.2.0 min version as Choco uses an older PowerShell Version (5.x)

from choco.

Comments (5)

laura-rodriguez avatar laura-rodriguez commented on May 30, 2024 2

@pauby I've made the changes you suggested, and the installation works as expected. Thanks a lot!

from choco.

TheCakeIsNaOH avatar TheCakeIsNaOH commented on May 30, 2024 1

I'm unsure if --use-system-powershell will use PowerShell Core if it's installed.

It will not use PowerShell Core, it will only use Windows PowerShell (via powershell.exe) when using system powershell.

Is there a way to force choco use a specific PowerShell version?

In this case, I think the best option would be to call pwsh.exe in chocolateyInstall.ps1 for any specific commands that need to be run in PowerShell Core. Use Start-ChocolateyProcessAsAdmin if the command needs administrator privileges.

from choco.

pauby avatar pauby commented on May 30, 2024 1

The issue with the package appears to be this line:

if ($PSVersionTable.PSVersion -lt [version]$minPSVersion) {
  throw "$moduleName module requires a minimum of PowerShell v$minPSVersion."
}

You're trying to detect the version of the PowerShell host and if it doesn't meet your minimum requirements, throwing an error. However, PowerShell (Core) is an optional install, on the majority of operating systems. But that doesn't stop your package from being installed. Your package doesn't need PowerShell Core. The module Okta.PowerShell does. But you're not running the module in the package. The package is simply the deployment method, so it doesn't matter what version of PowerShell it uses.

I do something like this for packages I maintain that require Java. I don't detect the Java version, but I do say 'This package requires Java' (the reason for this is that there are so many flavours and versions, it would be impossible to get it right all the time). . So I'd suggest the following:

# check PowerShell Core is available
if (Get-Command -Name 'pwsh.exe' -ErrorAction SilentlyContinue) {
    # we've confirmed PowerShell Core is available, retrieve the version of it
    $hostVersion = [Version]((pwsh.exe -Command { $PSVersionTable }).PSVersion)

    # does the version of PowerShell Core installed meet the minimum version we need?
    if ([Version]$hostVersion -lt [version]$minPSVersion) {
        # better to use 'Write-Warning' here
        throw "$moduleName module requires a minimum of PowerShell v$minPSVersion."
    }
}
else {
      # better to use 'Write-Warning' here
      throw "$moduleName module requires a minimum of PowerShell v$minPSVersion."
}

(I've briefly tested this code, and it works, but check it). (note that I wrote this to make it readable rather than concise - you can combine both if statements into one line)

What this does it check that PowerShell Core is installed. If not, it throws the exception. If it is installed, it runs the host and grabs the PSVersion from $PSVersionTable and then compares it to what you need.

A suggestion would be not to throw, but to use Write-Warning. The package can be installed without PowerShell Core installed and you should therefore allow the install, to take place with a warning that the module can't be run without them installing PowerShell Core. You could even help by providing them the command to install PowerShell Core: choco install powershell-core.

Or, the better way, is to simply add a dependency on a minimum version, 6.2.0, of powershell-core as a package dependency. That way you don't need any of the above as it will be taken care of for you.

from choco.

laura-rodriguez avatar laura-rodriguez commented on May 30, 2024 1

Thanks for all the suggestions here! They're super helpful.
I'll give adding powershell-core as a dependency and Pauby's script a try.

from choco.

pauby avatar pauby commented on May 30, 2024

Chocolatey CLI only supports Windows PowerShell as an internal host. I'm unsure if --use-system-powershell will use PowerShell Core if it's installed. @gep13 @AdmiringWorm may have the answer to that.

from choco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.