Closed laura-rodriguez closed 7 months ago
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.
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.
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.
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.
@pauby I've made the changes you suggested, and the installation works as expected. Thanks a lot!
Checklist
What You Are Seeing?
I'm preparing a Choco package for a PowerShell module that requires a minimum PS version of 6.2.0. When I try to test/install my Choco package locally, it fails as internally as the module installation uses "Host version is 5.1.22621.2506". I tried using the
--use-system-powershell
flag, but it doesn't work either for my case. Is there a way to force choco use a specific PowerShell version?Here's my redacted log:
Here's my chocolateyinstall.ps1 script:
What is Expected?
I want my installation script uses a specific PowerShell version if it's available.
How Did You Get This To Happen?
choco install okta.powershell --source ./ --use-system-powershell -y --debug --verbose --force
System Details
Installed Packages
Output Log