• Stars
    star
    176
  • Rank 216,987 (Top 5 %)
  • Language
    PowerShell
  • License
    MIT License
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A module to help other modules have settings

Build Status

The Configuration Module

Metadata commands for working with .psd1:

  • Manipulating metadata files
  • Extensible serialization of types
  • Built in support for DateTime, Version, Guid, SecureString, ScriptBlocks and more
  • Lets you store almost anything in readable metadata (.psd1) files
  • Serializing (Export) to metadata (.psd1) files
  • Deserializing (Import) from metadata (.psd1) files

Configuration commands for loading settings:

  • Ship default configuration files with your module
  • Automatically determines where to save your settings
    • Supports Windows roaming profiles
    • Supports XDG settings for Linux (and MacOS)
  • Allow users to configure settings using our commands, or your wrappers
  • Supports layered configurations:
    • Machine-level config
    • User override files
  • Supports automatic configuration of parameters values for a command
    • Reads Noun configuration files in your working directory
    • Filters only values which apply to the current command

It supports WindowsPowerShell, as well as PowerShell Core on Windows, Linux and OS X.

Installation

Install-Module Configuration

Usage

The Configuration module is designed to be used by other modules (or from scripts) to allow the storage of configuration data (generally, hashtables, but any PSObject).

In its simplest form, you add a Configuration.psd1 file to a module you're authoring, and put your default settings in it -- perhaps something as simple as this:

@{
    DriveName = "data"
}

Then, in your module, you import those settings in a function when you need them, or expose them to your users like this:

function Get-FaqConfig {
    Import-Configuration
}

Perhaps, in a simple case like this one, you might write a wrapper function so your users can get and set that one configuration option directly:

function Get-DataDriveName {
    $script:Config = Import-Configuration
    $config.DriveName
}

function Set-DataDriveName {
    param([Parameter(Mandatory)][String]$Name)

    @{ DriveName = $Name} | Export-Config
}

Of course, you could have imported the configuration, edited that one setting, and then exported the whole config, but you can also just export a few settings, because Import-Configuration supports a layered configuration. More on that in a moment, but first, let's talk about how this all works.

Versioning

Versioning your configuration is supported, but is only done explicitly (in Import-Configuration, Export-Configuration, and Get-ConfigurationPath). Whenever you need to change your module's configuration in an incompatible way, you can write a migration function that runs at import-time in your new version, something like this:

# Specify a script-level version number
$ConfigVersion = @{ Version = 1.1 }

function MigrateData {
    # Specify the version you want to migrate
    $OldVersion = @{ <# I didn't specify a version at first #> }

    # If there are no configuration files, migrate them
    if(!(Get-ConfigurationPath @ConfigVersion | Get-ChildItem -File)) {
        # Import the old config
        $oldConfig = Import-Configuration @OldVersion
        # Transform your configuration however you like
        $newConfig = @{ PSDriveName = $existing.DriveName }
        # Export the new config
        $newConfig | Export-Configuration @ConfigVersion
    }
}

# Call your migration function during module import
MigrateData

Then you just need to be sure you specify the @ConfigVersion whenever you call Import-Configuration elsewhere in your module.

Note that configuration files are not currently deleted by Uninstall-Module, so they are never automatically cleaned up.

How it works

The Configuration module works by serializing PowerShell hashtables or custom objects into PowerShell data language in a Configuration.psd1 file!

Configuration path

When you Export-Configuration you can set the -Scope, which determines where the Configuration.psd1 are stored:

  • User exports to $Env:LocalAppData or ~/.config/
  • Enterprise exports to $Env:AppData (the roaming path) or ~/.local/share/
  • Machine exports to $Env:ProgramData or /etc/xdg/

Note that the linux paths are controlled by XDG environment variables, and the default paths can be overriden by manually editing the Configuration module manifest.

Within that folder, the Configuration module root is "PowerShell," followed by either a company or author and the module name -- within which your configuration file(s) are stored.

From a module that uses Configuration, you can call the Get-ConfigurationPath command to get the path to that folder, and since the folder is created for you, you can use it store other files, like cached images, etc.

Layered Configuration

In addition to automatically determining the storage path, the configuration module supports layered configuration, so that you can have defaults you ship with your module, or configure default at the enterprise or machine level, and still allow users to override the settings. When you call Import-Configuration from within a module, it automatically imports all the available files and updates the configuration object which is returned at the end:

  1. First, it imports the default Configuration.psd1 from the module's folder.
  2. Then it imports machine-wide settings (e.g. the ProgramData folder)
  3. Then it imports the users' enterprise roaming settings (e.g. from AppData\Roaming)
  4. Finally it imports the users' local settings (from AppData\Local)

Any missing files are just skipped, and each layer of settings updates the settings from the previous layers, so if you don't set a setting in one layer, the setting from the previous layers persists.

However, it's up to individual users and module authors to take advantage of this..

Serialization

The actual serialization commands (with the Metadata noun) are: ConvertFrom, ConvertTo, Import and Export. By default, the Configuration serializer can handle a variety of custom PSObjects, hashtables, and arrays recursively, and has specific handling for booleans, strings and numbers, as well as Versions, GUIDs, and DateTime, DateTimeOffset, and even ScriptBlocks and PSCredential objects.

Important note: PSCredentials are stored using ConvertTo-SecureString, and currently only work on Windows. They should be stored in the user scope, since they're serialized per-user, per-machine, using the Windows Data Protection API.

In other words, it handles everything you're likely to need in a configuration file. However, it also has support for adding additional type serializers via the Add-MetadataConverter command. If you want to store anything that doesn't work, please raise an issue πŸ˜‰.

One little catch

The configuration module uses the caller's scope to determine the name of the module (and Company or Author name) that is asking for configuration. For this reason you normally just call Import-Configuration from within a function in your module (to make sure the callstack shows the module scope).

The very important side effect is that you must not change the module name nor the author of your module if you're using this Configuration module, or you will need to manually call Import-Configuration with the old information and then Export those settings to the new location (see the )

Using the cmdlets from outside a module

It is possible to use the commands to Import and Export the configuration for a module from outside the module (or from the main module body, instead of a function), simply pipe the ModuleInfo to Import-Configuration. To continue our example from earlier:

$Config = Get-Module DataModule | Import-Configuration
$Config.DriveName = "DataDrive"
Get-Module DataModule | Export-Configuration $Config

Note that if you look at the parameter sets for Import-Configuration you will find that you can also just pass the the -Author (or -CompanyName) and module -Name by hand, but you must be sure to get them exactly right, or you'll import nothing...

$Config = Import-Configuration -Name DataModule -Author HuddledMasses.org

Because of how easily this can go wrong, I strongly recommend you don't use this syntax -- but if you do, be aware that you must also specify the -DefaultPath if you want to load the default configuration file from the module folder.

A little history:

The Configuration module is something I first wrote as part of the PoshCode packaging module and have been meaning to pull out for awhile.

I finally started working on this while I work on writing the Gherkin support for Pester. That support was merged into Pester with the 4.0 release, and I'm using it for the tests in this module.

In any case, this module is mostly code ported from my PoshCode module as I develop the specs (the .feature files) and the Gherkin support to run them! Anything you see here has better than 95% code coverage in the feature and step files, which are executable via Invoke-Gherkin.

For the tests to work, you need to make sure that the module isn't already loaded, because the tests import it with the file paths mocked for testing:

Remove-Module Configuration -ErrorAction SilentlyContinue
Invoke-Gherkin -CodeCoverage *.psm1

More Repositories

1

PowerShellPracticeAndStyle

The Unofficial PowerShell Best Practices and Style Guide
2,198
star
2

ModuleBuilder

A PowerShell Module to help scripters write, version, sign, package, and publish.
PowerShell
442
star
3

Pansies

Powershell ANSI Escape Sequences, functions for colored output, etc.
C#
147
star
4

PSGit

A PowerShell implementation of Git, mapping git commands to Verb-Noun and objects.
PowerShell
103
star
5

PoshCode

PoshCode Modules for Packaging, Searching, Fetching
PowerShell
49
star
6

tldr

Simplified, community-driven, example-centric help pages for PowerShell
PowerShell
27
star
7

Metadata

Serialization for the PowerShell metadata format
PowerShell
19
star
8

PowerShellFAQ

A collection of frequently asked questions (and answers)
17
star
9

ScriptAnalyzer.PracticeAndStyle

Script Analyzer rules to support the PowerShell Practice and Style repository
PowerShell
15
star
10

CodeFlow

Functions for PowerShell code cleanup and generation
PowerShell
14
star
11

Azure-Pipelines

Job and Task templates for Azure Pipelines to be used as a submodule or resource
14
star
12

ErrorView

An attempt to simplify and customize error views
PowerShell
13
star
13

poshcode.github.io

The PoshCode.org script and video site archive
HTML
13
star
14

Actions

Github actions for PowerShell projects
PowerShell
11
star
15

Bundler

Bundling collections of modules for specific purposes
PowerShell
8
star
16

ModuleBuilderTask

An Azure Pipelines task for running ModuleBuilder
2
star
17

VirtualUserGroupFiles

Files from discussions in the discord/slack/irc virtual user group
PowerShell
1
star
18

Tasks

Shared Invoke-Build Tasks ...
PowerShell
1
star
19

CommunityFeed

Feed tool that creates pages for all Powershell Video Topics
PowerShell
1
star