Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Examples/IniConfigDemo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# Load the default configuration file in INI format
$Config = Get-ScriptConfig -Format INI

# Access the configuration settings
Write-Host "String :" $Config.MyString
Write-Host "Integer Positive :" $Config.MyIntegerPositive
Write-Host "Integer Negative :" $Config.MyIntegerNegative
Write-Host "Boolean True :" $Config.MyBooleanTrue
Write-Host "Boolean False :" $Config.MyBooleanFalse
Write-Host "Array :" "@(" (($Config.MyArray | ForEach-Object { '"{0}"' -f $_ }) -join ', ') ")"
Write-Host "Hashtable :" "@{" (($Config.MyHashtable.GetEnumerator() | ForEach-Object { '{0} = "{1}"' -f $_.Name, $_.Value }) -join '; ') "}"
9 changes: 9 additions & 0 deletions Examples/IniConfigDemo.ps1.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MyString=This is a test INI config file!
MyIntegerPositive=42
MyIntegerNegative=-153
MyBooleanTrue=True
MyBooleanFalse=False
MyArray[]=Lorem
MyArray[]=Ipsum
MyHashtable[Foo]=Bar
MyHashtable[Hello]=World
12 changes: 12 additions & 0 deletions Examples/JsonConfigDemo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# Load the default configuration file in JSON format
$Config = Get-ScriptConfig -Format JSON

# Access the configuration settings
Write-Host "String :" $Config.MyString
Write-Host "Integer Positive :" $Config.MyIntegerPositive
Write-Host "Integer Negative :" $Config.MyIntegerNegative
Write-Host "Boolean True :" $Config.MyBooleanTrue
Write-Host "Boolean False :" $Config.MyBooleanFalse
Write-Host "Array :" "@(" (($Config.MyArray | ForEach-Object { '"{0}"' -f $_ }) -join ', ') ")"
Write-Host "Hashtable :" "@{" (($Config.MyHashtable.GetEnumerator() | ForEach-Object { '{0} = "{1}"' -f $_.Name, $_.Value }) -join '; ') "}"
15 changes: 15 additions & 0 deletions Examples/JsonConfigDemo.ps1.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"MyString": "This is a test JSON config file!",
"MyIntegerPositive": 42,
"MyIntegerNegative": -153,
"MyBooleanTrue": true,
"MyBooleanFalse": false,
"MyArray": [
"Lorem",
"Ipsum"
],
"MyHashtable": {
"Foo": "Bar",
"Hello": "World"
}
}
12 changes: 12 additions & 0 deletions Examples/XmlConfigDemo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# Load the default configuration file in XML format
$Config = Get-ScriptConfig -Format XML

# Access the configuration settings
Write-Host "String :" $Config.MyString
Write-Host "Integer Positive :" $Config.MyIntegerPositive
Write-Host "Integer Negative :" $Config.MyIntegerNegative
Write-Host "Boolean True :" $Config.MyBooleanTrue
Write-Host "Boolean False :" $Config.MyBooleanFalse
Write-Host "Array :" "@(" (($Config.MyArray | ForEach-Object { '"{0}"' -f $_ }) -join ', ') ")"
Write-Host "Hashtable :" "@{" (($Config.MyHashtable.GetEnumerator() | ForEach-Object { '{0} = "{1}"' -f $_.Name, $_.Value }) -join '; ') "}"
18 changes: 18 additions & 0 deletions Examples/XmlConfigDemo.ps1.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Settings>
<Setting Type="string" Key="MyString" Value="This is a test XML config file!" />
<Setting Type="integer" Key="MyIntegerPositive" Value="42" />
<Setting Type="integer" Key="MyIntegerNegative" Value="-153" />
<Setting Type="boolean" Key="MyBooleanTrue" Value="true" />
<Setting Type="boolean" Key="MyBooleanFalse" Value="false" />
<Setting Type="array" Key="MyArray">
<Item Value="Lorem" />
<Item Value="Ipsum" />
</Setting>
<Setting Type="hashtable" Key="MyHashtable">
<Item Key="Foo" Value="Bar" />
<Item Key="Hello" Value="World" />
</Setting>
</Settings>
</Configuration>
99 changes: 99 additions & 0 deletions Functions/ConvertFrom-ScriptConfigIni.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<#
.SYNOPSIS
Convert the INI file content to a hashtable containing the configuration.

.DESCRIPTION
Convert the INI file content to a hashtable containing the configuration.

.PARAMETER Content
An array of strings with the INI file content. Each array item is a line.

.EXAMPLE
C:\> Get-Content -Path 'config.ini' | ConvertFrom-ScriptConfigIni
Use the pipeline input to parse the INI file content.
#>

function ConvertFrom-ScriptConfigIni
{
[CmdletBinding()]
param
(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true)]
[AllowEmptyString()]
[String[]] $Content
)

Write-Verbose "Parse script configuration file as INI format ..."

$Config = @{}

try
{
# Iterating each line and parse the setting
foreach ($Line in $Content)
{
switch -Wildcard ($Line)
{
# Comment
';*' {

break
}

# Array
'*`[`]=*'{
$Key = $Line.Split('[]=', 4)[0]
$Value = $Line.Split('[]=', 4)[3]

if ($null -eq $Config[$Key])
{
$Config[$Key] = @()
}

$Config[$Key] += $Value

break
}

# Hashtable
'*`[*`]=*' {
$Key = $Line.Split('[]=', 4)[0]
$Hash = $Line.Split('[]=', 4)[1]
$Value = $Line.Split('[]=', 4)[3]

if ($null -eq $Config[$Key])
{
$Config[$Key] = @{}
}

$Config[$Key][$Hash] = $Value

break
}

# String, Integer or Boolean
'*=*' {
$Key = $Line.Split('=', 2)[0]
$Value = $Line.Split('=', 2)[1]

try { $Value = [Int32]::Parse($Value) } catch { }

if ('True'.Equals($Value)) { $Value = $true }
if ('False'.Equals($Value)) { $Value = $false }

$Config[$Key] = $Value

break
}
}
}

Write-Output $Config
}
catch
{
throw "The configuration file content was in an invalid format: $_"
}
}
70 changes: 70 additions & 0 deletions Functions/ConvertFrom-ScriptConfigJson.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<#
.SYNOPSIS
Convert the JSON file content to a hashtable containing the configuration.

.DESCRIPTION
Convert the JSON file content to a hashtable containing the configuration.

.PARAMETER Content
An array of strings with the JSON file content. Each array item is a line.

.EXAMPLE
C:\> Get-Content -Path 'config.json' | ConvertFrom-ScriptConfigJson
Use the pipeline input to parse the JSON file content.
#>

function ConvertFrom-ScriptConfigJson
{
[CmdletBinding()]
param
(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true)]
[AllowEmptyString()]
[String[]] $Content
)

Write-Verbose "Parse script configuration file as JSON format ..."

$Config = @{}

try
{
# Join all lines into one string
$Content = $Content -join ''

# Parse the JSON content
$JsonContent = $Content | ConvertFrom-Json

# Extract all propeties from the json content
$JsonNodes = $JsonContent | Get-Member -MemberType NoteProperty

foreach ($JsonNode in $JsonNodes)
{
$Key = $JsonNode.Name
$Value = $JsonContent.$Key

# Hashtable / Other
if ($Value -is [System.Management.Automation.PSCustomObject])
{
$Config[$Key] = @{}

foreach ($Property in $Value.PSObject.Properties)
{
$Config[$Key][$Property.Name] = $Property.Value
}
}
else
{
$Config[$Key] = $Value
}
}

Write-Output $Config
}
catch
{
throw "The configuration file content was in an invalid format: $_"
}
}
85 changes: 85 additions & 0 deletions Functions/ConvertFrom-ScriptConfigXml.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<#
.SYNOPSIS
Convert the XML file content to a hashtable containing the configuration.

.DESCRIPTION
Convert the XML file content to a hashtable containing the configuration.

.PARAMETER Content
An array of strings with the XML file content. Each array item is a line.

.EXAMPLE
C:\> Get-Content -Path 'config.xml' | ConvertFrom-ScriptConfigXml
Use the pipeline input to parse the XML file content.
#>

function ConvertFrom-ScriptConfigXml
{
[CmdletBinding()]
param
(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true)]
[AllowEmptyString()]
[String[]] $Content
)

Write-Verbose "Parse script configuration file as XML format ..."

$Config = @{}

try
{
# Try to cast the content into an XmlDocument
$XmlContent = [Xml] $Content

# Extract all setting objects
$Settings = $XmlContent.Configuration.Settings.Setting

foreach ($Setting in $Settings)
{
switch ($Setting.Type)
{
# String
'string' {
$Config[$Setting.Key] = $Setting.Value
}

# Integer
'integer' {
$Config[$Setting.Key] = [Int32]::Parse($Setting.Value)
}

# Boolean
'boolean' {
$Config[$Setting.Key] = 'True' -eq $Setting.Value
}

# Array
'array' {
$Config[$Setting.Key] = @()
foreach ($Item in $Setting.Item)
{
$Config[$Setting.Key] += $Item.Value
}
}

# Hashtable
'hashtable' {
$Config[$Setting.Key] = @{}
foreach ($Item in $Setting.Item)
{
$Config[$Setting.Key][$Item.Key] = $Item.Value
}
}
}
}

Write-Output $Config
}
catch
{
throw "The configuration file content was in an invalid format: $_"
}
}
Loading