Skip to content

Instantly share code, notes, and snippets.

@AdmiringWorm
Last active March 20, 2021 14:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdmiringWorm/500f244c0098dddfd36474f63d3216af to your computer and use it in GitHub Desktop.
Save AdmiringWorm/500f244c0098dddfd36474f63d3216af to your computer and use it in GitHub Desktop.
PowerShell function not to use in Chocolatey community packages

There are some rules regarding which functions can be used in in Chocolatey packages hosted on the community repository. Basically, any function not available in PowerShell v2.0 can not be used (without a fallback helper). In this gist I want to keep a list of functions that were not available in PowerShell 2.0, or I have found that did not work as expected.

  • Convert-FromJson - while it is a common function nowadays, this was only added in PowerShell 3.0 and can not be used in a community package.
  • Get-ItemPropertyValue - added in PowerShell 5.0, and do not exist on the Chocolatey verifier.
  • Import-Certificate - Only available on some Server platforms (and Windows 10). As an alternative the linked fallback helper can be used instead
if (!(Test-Path Function:\Import-Certificate)) {
function Import-Certificate([string]$FilePath, [string]$CertStoreLocation) {
$certSplits = $CertStoreLocation -replace '^cert\:\\' -split '\\'
if (!($certSplits.Length -eq 2)) { throw "Unexpected certificate storage location" }
$certutil = Get-Command "certutil" | Select-Object -ExpandProperty Path
if (!$certutil) { throw "Path to certutil was not found" }
$arguments = @(
'-addstore'
$certSplits[1]
"$(Resolve-Path $FilePath)"
)
Start-ChocolateyProcessAsAdmin -ExeToRun $certutil -Statements $arguments
}
}
@TheCakeIsNaOH
Copy link

Mount-DiskImage and Dismount-DiskImage are not PowerShell v2 compatible, and they also require Windows 8/server 2012 or newer.
See https://chocolatey.org/packages/chocolatey-isomount.extension

Add-WindowsDriver is also Windows 8+, and PowerShell v3+, generally it is possible to use pnputil.exe as an alternative.

Same for most (or all?) of the other DISM functions: https://docs.microsoft.com/en-us/powershell/module/dism/?view=winserver2012-ps

This is not PowerShell exactly, but on Windows 7 (maybe 8 as well), but if you using pnputil, the newer /add-driver and /install flags are not available, so instead use -a and -i, which do exist on Windows 7.

@AdmiringWorm
Copy link
Author

hmm, those are a good point of cmdlets not being compatible.
However, I don't remember seeing it as used by any maintainers as far as I can remember and not sure if they qualify in that regard.

This is not PowerShell exactly, but on Windows 7 (maybe 8 as well), but if you using pnputil, the newer /add-driver and /install flags are not available, so instead use -a and -i, which do exist on Windows 7.

I was more interested in powershell cmdlets in this case, not executable flags that have changed.
But I will keep this in mind if/when I decide to include those as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment