Public/Utils/Get-ConfigurationData.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function Get-ConfigurationData {
  <#
      .SYNOPSIS
      Get-ConfigurationData will retrieve configuratin depending on the input

      .DESCRIPTION
      Currently JSON (.json) and PowerShell Data (.psd1) are supported.
      For JSON files possible output is Hashtable (default) and PSObject.
      For PSD1 files only Hashtable is currently supported.
      Using helpers function will return an object data from given configuration file

      .PARAMETER ConfigurationPath
      Path to JSON or psd1 file

      .EXAMPLE
      Get-ConfigurationData -ConfigurationPath C:\SomePath\Config.json -OutputType Hashtable
      Will read content of Config.json file and convert it to a HashTable.

      .EXAMPLE
      Get-ConfigurationData -ConfigurationPath C:\SomePath\Config.json -OutputType PSObject
      Will read content of Config.json file and convert it to a PS Object.

      .EXAMPLE
      Get-ConfigurationData -ConfigurationPath C:\SomePath\Config.psd1
      Will read content of Config.psd1 file and return it as a HashTable.

      .INPUTS
      Accepts string as paths to JSON or PSD1 files or folders with files

      .OUTPUTS
      Outputs a hashtable of key/value pair or PSObject.
  #>


  [CmdletBinding()]
  param (
    [Parameter(Mandatory, HelpMessage = 'Provide path for configuration file to read', Position = 0 )]
    [ValidateScript({Test-Path -Path $_})]
    [System.String[]]
    $ConfigurationPath,

    [Parameter(Mandatory = $false, HelpMessage = 'Select output type',Position = 1)]
    [ValidateSet('PSObject','HashTable')]
    [string]
    $OutputType='HashTable'

  )
  begin {
    $includeParams =  '*.psd1','*.json'
  }
  process{
    foreach ($path in $ConfigurationPath) {
      #-include only supported if -path to folder includes \* at the end
      #check if given $ConfigurationPath is a directory
      $pathType = Get-Item -Path $path
      switch($pathType.PSIsContainer) {
        $true {
          $configurationFile = Get-ChildItem -Path "$($path)\*" -Include $includeParams
        }
        $false {
          $configurationFile = Get-ChildItem -path $ConfigurationPath -Include $includeParams
        }
      }
      foreach ($file in $configurationFile) {
        #switch for different files supported
        switch ($file) {
          {$PSItem -match '.json'} {
            #switch for different file output
            switch($OutputType){
              'HashTable' {
                ConvertTo-HashtableFromJSON -Path $file
               }
              'PSObject' {
                ConvertTo-PSObjectFromJSON -Path $file
               }
            }
          }
          {$PSItem -match '.psd1'} {
            Write-Log -Info -Message "Reading configuration file from {$PSItem}"
            Import-LocalizedData -BaseDirectory (Split-Path $file -Parent) -FileName (Split-Path $file -Leaf)
          }
        }
      }
    }
  }
}