Chocolatey cpack参数传递问题:PackageParameter传递校验值失败
Hey there! Let's work through this Chocolatey package parameter issue with checksums — I remember stumbling over similar stuff when I was starting out too, so I feel your pain.
First, let's clarify two common scenarios you might be dealing with, since "passing a checksum as a package parameter" can mean two different things:
Scenario 1: Passing a checksum during package installation
If you're trying to dynamically provide a checksum when installing your custom package (to validate the installer file it downloads), here's the correct approach:
1. Update your install script to parse the parameter
In your chocolateyinstall.ps1, you need to explicitly grab and use the incoming package parameter. Add this code:
# Parse the package parameters passed during installation $packageParams = Get-PackageParameters # Extract the checksum value (case-insensitive, but keep names consistent for clarity) $targetChecksum = $packageParams.Checksum # Use the checksum when downloading your installer (adjust details to match your setup) Get-ChocolateyWebFile -PackageName "YourPackageName" ` -Url "https://example.com/your-installer.exe" ` -FileFullPath "$toolsDir\installer.exe" ` -Checksum $targetChecksum ` -ChecksumType sha256 # Change to sha1/md5 if that's your checksum type
2. Pass the parameter correctly during installation
When running the install command, use the /Key:Value syntax for package parameters:
choco install YourPackageName --package-parameters "/Checksum:abc123def4567890abcdef1234567890"
Scenario 2: Dynamically setting the checksum when creating the NuGet package
If you're trying to inject your precomputed checksum into the package during the choco pack step (instead of during installation), package parameters aren't the right tool for this. Instead, use a template nuspec and replace a placeholder via your batch script:
1. Create a nuspec template with a placeholder
<?xml version="1.0"?> <package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd"> <metadata> <id>YourPackageName</id> <version>1.0.0</version> <title>Your Custom Package</title> <authors>Your Name</authors> <summary>Short description of your package</summary> <description>Full description of your package</description> <tags>your tags here</tags> </metadata> <files> <file src="tools\**" target="tools" /> </files> <properties> <!-- Placeholder to replace with your computed checksum --> <checksum>__DYNAMIC_CHECKSUM__</checksum> </properties> </package>
2. Replace the placeholder in your batch script
Add this to your DOS batch file (right after your checksum calculation step):
@echo off setlocal enabledelayedexpansion :: Your existing code to compute the checksum (you confirmed this works!) set COMPUTED_CHECKSUM=abc123def4567890 :: Replace the placeholder in the nuspec file powershell -Command "(Get-Content 'YourPackageName.nuspec') -replace '__DYNAMIC_CHECKSUM__', '%COMPUTED_CHECKSUM%' | Set-Content 'YourPackageName.nuspec'" :: Now pack the package choco pack YourPackageName.nuspec
3. Pull the checksum from the nuspec in your install script
In chocolateyinstall.ps1, retrieve the checksum you injected:
$nuspecPath = Join-Path $env:ChocolateyPackageFolder "$env:ChocolateyPackageName.nuspec" $checksum = (Select-Xml -Path $nuspecPath -XPath "//properties/checksum").Node.InnerText # Use the checksum in your download command as before
Quick Troubleshooting Tips
- Double-check parameter syntax: It's
/Checksum:Value, not--Checksum Value— that's a super common mix-up. - Match checksum types: If you generated an SHA-1 checksum, don't tell Chocolatey to use SHA-256 in your script.
- Don't forget
Get-PackageParameters: If you're in scenario 1, skipping this command means your script will never see the parameter you passed.
内容的提问来源于stack exchange,提问作者speKc

