publish PSModuleBase post (#17)

This commit is contained in:
OCram85 2022-04-26 09:35:14 +02:00 committed by GitHub
parent 2d918c7c62
commit 0f251715d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
--- ---
title: 'PowerShell Module Base for Config Files' title: 'How to get your PowerShell Module Base root path'
date: 2022-03-21T09:14:41+01:00 date: 2022-03-21T09:14:41+01:00
draft: true #draft: true
categories: ['PowerShell'] categories: ['PowerShell']
tags: ['ModuleBase', 'config'] tags: ['ModuleBase', 'config']
@ -23,17 +23,19 @@ tags: ['ModuleBase', 'config']
## 🖼️ Intro ## 🖼️ Intro
Sometimes you want to use a basic config file for your module. This config file should be used to define basic This article explains with a practical example how to determine and use the current module base path.
settings without any user specific content. This file could be placed into your PowerShell Module folder.
Sometimes you want to use a basic config file for your module. This config file could be used to define basic
settings for your module. These module wide default settings should placed and shipped within your module.
Therefore you can use the automatic variable `$MyInvocation`, especially with its properties Therefore you can use the automatic variable `$MyInvocation`, especially with its properties
`$MyInvocation.MyCommand.Module.ModuleBase`. This returns the full path to your current module base folder, which `$MyInvocation.MyCommand.Module.ModuleBase`. This returns the full path to your current module base folder, which
can be used to join a path for your config file. can be used by your function to join a path for your config file.
## 📑 `.\config.psd1` config file ## 📑 `.\config.psd1` config file
Let's assume you start a new module and you need multiple config keys to work with. So you usually create a Let's assume you start a new module and you need multiple config keys to work with. So you usually create a
json or powershell data based config file: **JSON** or **P**ower**S**hell **D**ata format based config file:
```powershell ```powershell
{ {
@ -53,8 +55,8 @@ json or powershell data based config file:
## 🔎 `Get-ConfigValue` helper function ## 🔎 `Get-ConfigValue` helper function
Now you can use `$MyInvocation.MyCommand.Module.ModuleBase` with a helper function to parse the path to your config Now you can use `$MyInvocation.MyCommand.Module.ModuleBase` with a helper function, to parse the path to your config
file and return your needed values: file, and return the stored default values:
```powershell ```powershell
function Get-ConfigValue { function Get-ConfigValue {
@ -62,14 +64,14 @@ function Get-ConfigValue {
.SYNOPSIS .SYNOPSIS
Returns the value of a given config file key. Returns the value of a given config file key.
.PARAMETER ByKey .PARAMETER FromKey
Config file key. Config file key.
.OUTPUTS .OUTPUTS
[string] [string]
.EXAMPLE .EXAMPLE
Get-ConfigValue -ByKey 'OutputLevel' Get-ConfigValue -FromKey 'OutputLevel'
.NOTES .NOTES
Private module helper function. Used by other function within your module. Private module helper function. Used by other function within your module.
@ -79,26 +81,27 @@ function Get-ConfigValue {
[OutputType([string])] [OutputType([string])]
param ( param (
[Parameter(Mandatory = $true, HelpMessage = 'Existing key from config file.')] [Parameter(Mandatory = $true, HelpMessage = 'Existing key from config file.')]
[string]$ByKey [string]$FromKey
) )
begin { } begin { }
process { process {
$ErrorActionPreference = 'Stop'
$ModuleBase = $MyInvocation.MyCommand.Module.ModuleBase $ModuleBase = $MyInvocation.MyCommand.Module.ModuleBase
$ConfigFile = Join-Path -Path $ModuleBase -ChildPath 'config.psd1' $ConfigFile = Join-Path -Path $ModuleBase -ChildPath 'config.psd1'
if (Test-Path -Path $ConfigFile) { if (Test-Path -Path $ConfigFile) {
try { try {
$Config = Import-PowerShellDataFile -Path $ConfigFile $Config = Import-PowerShellDataFile -Path $ConfigFile
Write-Output $Config.$ByKey Write-Output $Config.$FromKey
} }
catch { catch {
Write-Error -Message $_.Exception.Message -ErrorAction Stop Write-Error -Message $_.Exception.Message
} }
} }
else { else {
Write-Error -Message 'Config file not found!' -ErrorAction 'Stop' Write-Error -Message 'Config file not found!'
} }
} }
@ -108,12 +111,12 @@ function Get-ConfigValue {
## 💭 Final Thoughts ## 💭 Final Thoughts
All you need to to is using the `Get-ConfigValue -ByKey '<example key>'` in your functions to get the any value All you need to to is using the `Get-ConfigValue -FromKey '<example key>'` in your functions to get any value
defined in you config file. defined in your config file.
As far as I know, that's the simplest way to get your module root and using it with a config file. As far as I know, that's the simplest way to get your module root and using it with a config file.
{{< alert >}} {{< alert >}}
Keep in mind not to store any sensitive data in you config file. User specific data should also be stored in a user Do not to store any sensitive data in you config file. User specific data should also be stored in a user
context and not in a global module wide config. context and not in a global module wide config file.
{{< /alert >}} {{< /alert >}}