For an alternative to XML configuration, if you are flexible to use other type of configuration. I suggest using a global PS configuration file and here is how:
Create a Powershell configuration file ( E.g. Config.ps1 ), then put all configuration as global variable and init it as a first step so that configuration values should be available in your script context.
Benefit of this approach is that various type of data structure such as scalar variable, collections and hashes can be used in Config.ps1 PS file and referenced easily in the PS code.
the following is an example in action:
Here is the C:\Config\Config.ps1 file:
$global:config = @{
Var1 = "Value1"
varCollection = @{
item0 = "colValue0"
item1 = "colValue1"
item2 = "colValue2"
}
}
Then load the functions/variables from Config.ps1 file in this module C:\Module\PSModule.psm1, like so:
$scriptFiles = Get-ChildItem "$PSScriptRoot\Config\*.ps1" -Recurse
foreach ($script in $scriptFiles)
{
try
{
. $script.FullName
}
catch [System.Exception]
{
throw
}
}
Lastly, initialization script contains this one line below: ( C:\Init.ps1 ).
Import-Module $PSScriptRoot\Module\PSModule.psm1 -Force
After running the Init.ps1. global:config variable will be available in your script context. Here is the output: