Use Chocolatey helper functions in PowerShell
Asked Answered
G

3

7

The Chocolatey helper functions are intended to be used in Chocolatey package scripts.

NOTE: These scripts are for package scripts, not for use directly in PowerShell. This is in the create packages section, not the using Chocolatey section.

There are a few useful helper functions though, such as Install-ChocolateyPinnedTaskBarItem, which I would like to try in a PowerShell console.

Is there any way I can run those helper functions in a PowerShell console?

Gamosepalous answered 10/9, 2018 at 12:55 Comment(1)
What is the problem you are facing? I don't get why you can't just run those functions, just because it says you shouldn't.Moldau
U
6

Stuff you want to import is located in file C:\ProgramData\chocolatey\helpers\chocolateyInstaller.psm1

But this might also depend on user - someone might override chocolatey installation and put it in other folder.

But you want to use same helper functions only if necessary, not otherwise.

You can use following script at the beginning of your .ps1 script:

if( -not ( get-command Install-ChocolateyPackage -erroraction silentlycontinue ) ) {
    Write-Host "Importing chocolateyInstaller.psm1..."
    Import-Module C:\ProgramData\chocolatey\helpers\chocolateyInstaller.psm1 #-Verbose
}

Here I'm checking if function Install-ChocolateyPackage was already imported - if not - then import by default path.

Remove Write-Host and #- Verbose comment after you don't need those.

Underclassman answered 23/6, 2020 at 11:34 Comment(0)
L
2

For the most part, these Helper Functions should "just" work. You can load the Chocolatey PowerShell Modules into your current PowerShell Session and make use of the Helper Functions.

In fact, there is actually a Chocolatey Extension that does just that, to help with the creation of Chocolatey Packages:

https://chocolatey.org/packages/chocolateypowershell

However, it should be noted that it is possible that some variables, and other things, used within these PowerShell functions might not be set correctly out with execution of Chocolatey itself. When executed, Chocolatey actually creates it's own PowerShell host, and sets things up the way that they need to be for successful execution. It could be that directly loading the PowerShell modules, doesn't set up everything that is required for all things to work exactly as they are intended.

Having said that, the simpler Helper Functions, for example the Install-ChocolateyPinnedTaskBarItem that you mentioned, should work fine.

Liability answered 10/9, 2018 at 13:21 Comment(0)
R
1

Yes, I whant the same thing, to test a few useful helper functions in a PowerShell console.

For what we are trying to do, you will need to first do an Import-Module on the Chocolatey module, to bring it into the current PowerShell session. You can find this here: C:\ProgramData\chocolatey\lib\chocolatey\tools\chocolateyInstall\helpers\chocolateyInstaller.psm1

Import-Module C:\ProgramData\chocolatey\lib\chocolatey\tools\chocolateyInstall\helpers\chocolateyInstaller.psm1       

That should allow you to test the script directly and all the others chocolatery Helpers Reference

Another alternative, is to install the chocolateypowershell, you could run the install of the package, and then run the chocolatey helper functions to see if it works as well. https://chocolatey.org/packages/chocolateypowershell

Roughandready answered 10/9, 2018 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.