Public/Utils/ConvertTo-HashtableFromJSON.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
function ConvertTo-HashtableFromJSON {
    #requires -Version 3.0
    <#
      .SYNOPSIS
      Retrievies json file from disk and converts to hashtable.

      .DESCRIPTION
      Reads file given as Path parameter and using ConvertTo-HashtableFromPsCustomObject converts it to a hashtable.

      .PARAMETER Path
      Path to a json file.

      .EXAMPLE
      ConvertTo-HashtableFromJSON -Path c:\somefile.json
      Will read somefile.json and convert it to custom hashtable.

      .INPUTS
      Path to a json file (string).

      .OUTPUTS
      Custom Hashtable.
  #>




    [CmdletBinding()]
    [OutputType([Hashtable])]
    param (
        [Parameter(Mandatory = $true,
            Position = 0, HelpMessage = 'Path to json file',
            ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateScript( {Test-Path -Path $_ -PathType 'Leaf' -Include '*.json' })]
        [string]
        $Path
    )

    Process {
        Write-Log -Info -Message "Reading configuration file from {$Path}"
        $content = Get-Content -LiteralPath $path -ReadCount 0 -Raw | Out-String
        $pscustomObject = ConvertFrom-Json -InputObject $content
        $hashtable = ConvertTo-HashtableFromPsCustomObject -psCustomObject $pscustomObject
        $hashtable
    }
}