Created
September 14, 2017 12:17
-
-
Save cernoel/87ca9817a9b1c8702e200e76e2999b12 to your computer and use it in GitHub Desktop.
PowerShell JSON Config-File-Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Module | |
filter timestamp {"$(Get-Date -Format yyyy-MM-dd-HH:mm:ss.fff): $_"} | |
$CConfig = @{} | |
function Import-CConfig{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[ValidateNotNullorEmpty()] | |
[String] $FilePath | |
) | |
BEGIN {} | |
PROCESS {} | |
END { | |
Write-Host("Import: " + $FilePath | timestamp) | |
try{ | |
$JSONfile = Get-Content -Raw -Path $FilePath | ConvertFrom-Json | |
} catch { | |
Write-Warning("Error import Config: " + $FilePath | timestamp) | |
} | |
foreach($entry in $JSONfile.Config){ | |
if($entry.Name -and $entry.Value){ | |
Set-CConfig -Name $entry.Name -Value $entry.Value | |
} else { | |
Write-Warning("Error import Entry." | timestamp) | |
} | |
} | |
} | |
} | |
function Get-CConfig{ | |
param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[ValidateNotNullorEmpty()] | |
[String] $Name | |
) | |
BEGIN {} | |
PROCESS {} | |
END {return [string]$CConfig[$Name]} | |
} | |
function Set-CConfig{ | |
param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[ValidateNotNullorEmpty()] | |
[String] $Name, | |
[Parameter(Mandatory=$true,Position=2)] | |
[ValidateNotNullorEmpty()] | |
[String] $Value | |
) | |
BEGIN {} | |
PROCESS {} | |
END { | |
if($Name){ | |
if(!$Value){ | |
Host-Warning("Value empty: " + $Name | timestamp) | |
} else { | |
if($CConfig.ContainsKey($Name)){ | |
$CConfig.Remove($Name) | |
$CConfig.Add($Name, $Value) | |
} else { | |
$CConfig.Add($Name, $Value) | |
} | |
} | |
} else { | |
Host-Warning("CConfig: No Name, no Entry.") | |
} | |
} | |
} | |
Export-ModuleMember -function Set-C* | |
Export-ModuleMember -function Get-C* | |
Export-ModuleMember -function Import-C* | |
#load a default config... | |
#Import-CConfig(".\global.config") | |
#Example global.config | |
#{"config": [ | |
# {"Name": "version", "Value": "0.0.1"}, | |
# {"Name": "SchnittstellenDateiPfad", "Value": "C:\\Daten\\Import\\data\\"}, | |
# {"Name": "TestDateiPfad", "Value": "C:\\Daten\\Import\\test\\"}, | |
# {"Name": "DateiEndung", "Value": ".csv"} | |
# ] | |
#} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment