(#3344) Adds ParameterBlock Handling to Chocolatey Script Runner#3682
Open
JPRuskin wants to merge 6 commits intochocolatey:developfrom
Open
(#3344) Adds ParameterBlock Handling to Chocolatey Script Runner#3682JPRuskin wants to merge 6 commits intochocolatey:developfrom
JPRuskin wants to merge 6 commits intochocolatey:developfrom
Conversation
JPRuskin
commented
Jun 5, 2025
44 tasks
40f5979 to
f60bd70
Compare
vexx32
reviewed
Jun 9, 2025
Member
vexx32
left a comment
There was a problem hiding this comment.
I'll have to come back and review the tests added in a bit, but some initial comments. :3
Adds a basic function to the helper functions that parses a script file for parameters, and compares that list to available Package Parameters. If there are matches, it returns a hashtable with matching names and their values. This can then be used in the chocolateyScriptRunner to pass parameters into the param block. The methods used all work back to PowerShell v3, though without any plan for backwards compatibility (e.g. considerations for using an extension package/module) it's hard to see this being that popular. (chocolatey#3344) Adds Tests for Get-ScriptParameters Adding some tests to check the conversion from PowerShell to C# cmdlet.
Rewrite of the Get-PackageParameter(s) into C# cmdlet, though there will be further changes made to refactor it into a helper method in a later commit.
Updates included SMA to 6.2.9200.24523. This is now equivalent to a version shipped on Windows Server 2012 (the earliest OS Chocolatey products support). This is necessary for the more advanced use of the AST parser, and may allow us to benefit from upgrades elsewhere.
It was pointed out that this PR should be targeted at including new helper functions into the C# conversion efforts. This converts Get-PackageParameter to use a helper class (shared between this and the new cmdlet) and adds Get-PackageScriptParameters to the Chocolatey.PowerShell compiled module.
…ion scriptfile As it was pointed out that removing a script from the helpers module was a breaking change, I have reverted that change. The PackageParameter helper remains, used by PackageScriptParameters, but there are no longer any removals from the user contract.
Also refactors the helper method for package parameters to enable logging of sensitive parameter names (not values).
Member
Author
|
I believe this is no longer a breaking change (as it doesn't remove or change any PowerShell helpers), makes all changes to helper functionality in C#, and doesn't break any business promises. I suspect I've missed the boat on getting it merged into 2.5.0, though! Shocking. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description Of Changes
This PR introduces the ability for ChocolateyInstall, ChocolateyBeforeModify, and ChocolateyUninstall scripts to have and utilise PowerShell
param()blocks to handle parameters.It does this by adding a function (
Get-PackageScriptParameters), which calculates what parameters a script has and passes any parameters found by the existing functionGet-PackageParametersthat match the parameter names to the script. Optionally, we could handle aliases, but this implementation does not yet.The new function is called within the ChocolateyScriptRunner before invoking the script, and the resultant hashtable is "splatted" with the call. When no matching parameters are found (on the script and in packageparameters), this results in no change to behaviour.
It's worth noting that though this does allow for usage of parameter attributes and validation, such as


[Parameter(Mandatory)],[ValidateSet()]and the like, Chocolatey can currently swallow some useful output (e.g. with SomeString being a mandatory parameter):vs the PowerShell default:
This could possibly be improved in the future.
It adds the functionality, but doesn't remove the existing parameter handling - so all existing packages will continue working.
SMA Update
As a part of this, because of the use of the
System.Management.Automation.Languageto assess the param block, theSystem.Management.Automationassembly present in thelib/powershellfolder was updated from a version matching the version shipped with PowerShell 2.0 (best guess) to a version from our earliest currently supported version of Windows - 2012.This DLL isn't shipped in any of our builds; it's just used to ensure our code uses methods et al that are supported by the specific DLL we have embedded in the repository.
Specifically, I grabbed this from the uncertain/fun provenance of a Windows Server 2012 VM, in the folder
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35. There's not a similar version on nuget.org, annoyingly, which only has versions from 2018 onwards starting at 6.0.4.There is an argument that this is a breaking change, as it may no longer work on machines we don't support - but that "break" has already happened. We do not support Windows versions prior to 2012 (and that only in Azure). Everything else is additive-only, and backwards compatible via a "lovely" new extension I'm looking to publish.
Motivation and Context
This allows for standardisation of how package maintainers handle parameters within their scripts, and helps Chocolatey Community Repository users and moderators to more easily understand what parameters can be used, what default values are used, and what (if any) validation is in place.
Testing
Set-ExecutionPolicy RemoteSigned Process -Forceand Install with.\Install.ps1 -ChocolateyDownloadUrl .\Path\To\chocolatey.nupkgchoco install cloudflared --confirmchoco install Temurin21jre --params="/ADDLOCAL=FeatureMain,FeatureEnvironment,FeatureJarFileRunWith,FeatureJavaHome" --confirmchoco install testextension --params='/SomeString:A String Value' --confirm(some massaging required for source)Operating Systems Testing
I also have an extension package that adds the same functionality to any other extension-supporting version of Chocolatey CLI. This hopefully means that this can be an acceptable thing to use for Community packages.
I am very excited about this, despite the code being utterly despicable.
I have had to slightly revise my backwards compatibility happiness - it doesn't work with the earliest mention of Chocolatey extensions (0.9.10) because

Get-PackageParametersdoesn't exist at that point. I could fix this by packing a backup version fo the function in the extension, but I feel like supporting back to the 0.10.8 is enough for any unreasonable expectation:Change Types Made
[ ] Bug fix (non-breaking change).[ ] Breaking change (fix or feature that could cause existing functionality to change).See SMA update.[ ] Documentation changes.Change Checklist
[ ] Requires a change to the documentation.[ ] Documentation has been updated.Related Issue
Resolves #3344