jberezanski / ChocolateyPackages

Chocolatey packages maintained by me
MIT License
83 stars 51 forks source link

Add support for powershell 7 #138

Closed tapika closed 1 year ago

tapika commented 1 year ago

Add support for powershell 7 / swupd fork

jberezanski commented 1 year ago

What is the story behind this change? Right now, Chocolatey uses Windows PowerShell and I'm not aware of any specific plans or deadlines for switching to PowerShell Core.

tapika commented 1 year ago

My own fork https://github.com/tapika/swupd supports powershell v7 and I'm continuing to improve the system.

tapika commented 1 year ago

I have tested my changes also with choco, so should be backwards compatible.

tapika commented 1 year ago

Any progress ? This is at least blocking my work currently - wanted to start to upgrade choco / gui choco packages.

jberezanski commented 1 year ago

I see.

Well, first, a disclaimer: my packages are designed for and intended to be used with the real Chocolatey only. I will accept fully compatible changes such as these, but I will not put any effort towards testing with anything other than the original choco.exe when making my own modifications.

With that out of the way, I'm OK with switching to Get-CimInstance. However, I don't want to encode assumptions about PowerShell version numbers. Rather, the script should test for the presence of the command:

if ($null -ne (Get-Command -Name Get-CimInstance -ErrorAction SilentlyContinue))
{ ... use Get-CimInstance ...

The package version should also be bumped to 1.0.5 and the changelog updated.

tapika commented 1 year ago

When I started testing - I've noticed that Get-CimInstance works out of box in choco - then googled a little bit, and it looks like this command was present even in powershell v2. So if-else variation is not needed - can just use newer version.

Updated version and changelog.

Do you do package publishing ?

jberezanski commented 1 year ago

No, PS 2.0 definitely does not have the CIM cmdlets. They were added as one of the major features of PS 3.0 (official introduction). A quick test on Win7 confirms this:

PS C:\> cmd /c ver

Microsoft Windows [Version 6.1.7601]
PS C:\> $PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5420
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

PS C:\> Get-Command Get-CimInstance
Get-Command : The term 'Get-CimInstance' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:12
+ Get-Command <<<<  Get-CimInstance
    + CategoryInfo          : ObjectNotFound: (Get-CimInstance:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

PS C:\> cinst -y KB2454826
Chocolatey v1.1.0
Installing the following packages:
KB2454826
By installing, you accept licenses for the packages.
Progress: Downloading KB2454826 1.0.4... 100%

KB2454826 v1.0.4 [Approved]
kb2454826 package files install completed. Performing other installation steps.
ERROR: The term 'Get-CimInstance' is not recognized as the name of a cmdlet, function, script file, or operable program.
 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The install of kb2454826 was NOT successful.
Error while running 'C:\ProgramData\chocolatey\lib\KB2454826\Tools\ChocolateyInstall.ps1'.
 See log for details.

Chocolatey installed 0/1 packages. 1 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Failures
 - kb2454826 (exited -1) - Error while running 'C:\ProgramData\chocolatey\lib\KB2454826\Tools\ChocolateyInstall.ps1'.
 See log for details.

So the command existence test is required.

One other thing: commit titles in this repo follow the pattern <package>: <change>, please adjust accordingly.

Do you do package publishing ?

Yes, once the PR is ready I will merge it and publish the updated package.

tapika commented 1 year ago

Ok, most probably I have upgraded powershell on my windows 7, that's why have CimCmdlets as built-in in powershell. Had to manually remove C:\Windows\System32\WindowsPowerShell\v1.0\Modules\CimCmdlets folder to test choco.

Hopefully now works ?

jberezanski commented 1 year ago

Yes, everything seems to be in order now.

jberezanski commented 1 year ago

I've published the updated extension. Thank you for your contribution!

tapika commented 1 year ago

Thanks. Waiting for 1.0.5 to be published, need to wait probably.

jberezanski commented 1 year ago

https://community.chocolatey.org/packages/chocolatey-windowsupdate.extension/1.0.5 - awaiting moderation. Might take longer than usual due to an ongoing issue with the virus scanner.