Manage software on Windows with the package-method-winget module

Posted by Craig Comstock
September 2, 2024

As a developer and user of CFEngine I want to use policy to manage the software on my systems so that I can switch operating systems, distributions, computers and have all my normal tools available wherever I go.

Towards this end I searched for a Windows package manager and found one in winget. I showed a prototype in Agent Is In - Episode 37 - Windows package management as well as refined the whole process in Agent Is In - Episode 40 - Windows module workshop.

Caveat

In working with winget I found that it is primarily supported on desktop systems. I will be researching this more in the future to come up with ways to manage software on Windows server systems with CFEngine policy.

From the winget documentation:

The WinGet command line tool enables users to discover, install, upgrade, remove and configure applications on Windows 10 and Windows 11 computers. This tool is the client interface to the Windows Package Manager service.

Add the module

Here I will show how to add this new package-method-winget module via the cfbs command line client. It is also possible to add it with the Build app in Mission Portal.

$ cfbs add package-method-winget
The added module 'package-method-winget' accepts user input.
  Do you want to add it now? [yes/y/NO/n] y
Collecting input for module 'package-method-winget'
Would you like to accept source agreements for winget packages promises?
  [yes|no]: y
Would you like to accept package agreements for winget packages promises?
  [yes|no]: y
Would you like to allow this module to change PowerShell Execution Policy
  to LocalMachine:Unrestricted? [yes|no]: y
The default commit message is:

        Added 2 modules

         - Added module 'powershell-execution-policy'
         - Added module 'package-method-winget'
         - Added input for module 'package-method-winget'

Edit it? [yes/y/NO/n] 
Committing using git:

[main 8d1b96e] Added 2 modules
 2 files changed, 77 insertions(+)
 create mode 100644 package-method-winget/input.json

This module has some important inputs which require answers in order to enable the package method to function properly.

Would you like to accept source agreements for
  winget packages promises? [yes|no]: y
Would you like to accept package agreements for
  winget packages promises? [yes|no]: y

These two opt-in questions relate to ensuring that the package method can operate automatically without causing install commands to wait for user input.

Would you like to allow this module to change PowerShell Execution Policy
  to LocalMachine:Unrestricted? [yes|no]: y

This opt-in is required if winget and the Microsoft.WinGet.Client PowerShell module is not installed. The install can be automated by the policy by it running two PowerShell scripts but if the LocalMachine Execution Policy is set to the default of Undefined CFEngine will not be able to run these scripts and install the needed software.

Usage

At this point you can write some packages promises:


packages:
  "Microsoft.WindowsTerminal"
    package_method => winget,
    package_policy => "add";

More on the way

Look for more Windows related modules in the upcoming month. During the module workshop mentioned above I also showed progress on modules for

  • Inventorying and managing Windows Capabilities
  • Inventorying and managing Windows Optional Features

Let us know how you use CFEngine with Windows and how we can help you do more!

Questions?

If you have questions or need help, reach out on the mailing list or GitHub discussions. If you have a support contract, feel free to open a ticket in our support system.