|
1 | | -# ------------------------------------------------------------------------------ |
2 | | -# Properties parsing function |
3 | | -# ------------------------------------------------------------------------------ |
4 | 1 | function Read-Properties { |
5 | | - <# |
6 | | - .SYNOPSIS |
7 | | - Parse properties file |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Parse properties file |
8 | 5 |
|
9 | | - .DESCRIPTION |
10 | | - Parse properties file to generate configuration variables |
| 6 | + .DESCRIPTION |
| 7 | + Parse properties file to generate configuration variables |
11 | 8 |
|
12 | | - .PARAMETER Path |
13 | | - The patch parameter corresponds to the path to the property file to read. |
| 9 | + .PARAMETER Path |
| 10 | + [String] The patch parameter corresponds to the path to the property file to read. |
14 | 11 |
|
15 | | - .PARAMETER Section |
16 | | - [Switch] The Section parameter indicates if properties should be grouped depending on |
17 | | - existing sections in the file. |
| 12 | + .PARAMETER Section |
| 13 | + [Switch] The Section parameter indicates if properties should be grouped depending on existing sections in the file. |
18 | 14 |
|
19 | | - .OUTPUTS |
20 | | - [System.Collections.Specialized.OrderedDictionary] Read-Properties returns an |
21 | | - ordered hash table containing the content of the property file. |
| 15 | + .PARAMETER Metadata |
| 16 | + [Switch] The metadata parameter indicates that the value (data) as well as the description and section (metadata) should be returned. This does not apply is the section switch is enabled. |
22 | 17 |
|
23 | | - .EXAMPLE |
24 | | - Read-Properties -Path ".\conf\default.ini" -Section |
| 18 | + .OUTPUTS |
| 19 | + [System.Collections.Specialized.OrderedDictionary] Read-Properties returns an ordered hash table containing the content of the property file. |
25 | 20 |
|
26 | | - In this example, Read-Properties will parse the default.ini file contained |
27 | | - in the .\conf directory and generate an ordered hashtable containing the |
28 | | - key-values pairs. |
29 | | - #> |
30 | | - [CmdletBinding ()] |
31 | | - Param ( |
32 | | - [Parameter ( |
33 | | - Position = 1, |
34 | | - Mandatory = $true, |
35 | | - HelpMessage = "Path to the property file" |
36 | | - )] |
37 | | - [ValidateNotNullOrEmpty ()] |
38 | | - [String] |
39 | | - $Path, |
40 | | - [Parameter ( |
41 | | - Position = 3, |
42 | | - Mandatory = $false, |
43 | | - HelpMessage = "Define if section headers should be used to group properties or be ignored" |
44 | | - )] |
45 | | - [Switch] |
46 | | - $Section |
47 | | - ) |
48 | | - Begin { |
49 | | - # Get global preference variables |
50 | | - Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState |
51 | | - # Instantiate variables |
52 | | - $Properties = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary" |
53 | | - $Sections = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary" |
54 | | - $Header = $null |
55 | | - $errors = 0 |
56 | | - } |
57 | | - Process { |
58 | | - # Check that the file exists |
59 | | - if (Test-Path -Path $Path) { |
60 | | - $ListOfProperties = Get-Content -Path $Path |
61 | | - $LineNumber = 0 |
62 | | - # Read the property file line by line |
63 | | - foreach ($Property in $ListOfProperties) { |
64 | | - $LineNumber += 1 |
65 | | - # If properties have to be grouped by section |
66 | | - if ($Section) { |
67 | | - # If end of file and section is open |
68 | | - if ($LineNumber -eq $ListOfProperties.Count -And $Header) { |
69 | | - if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") { |
70 | | - $Property = Read-Property -Property $Property |
71 | | - if ($Property.Count -gt 0) { |
72 | | - $Sections.Add($Property.Key, $Property.Value) |
73 | | - } else { |
74 | | - Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path" |
75 | | - } |
76 | | - } |
77 | | - $Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep |
78 | | - $Properties.Add($Header, $Clone) |
79 | | - } elseif ($Property[0] -eq "[") { |
80 | | - # If previous section exists add it to the property list |
81 | | - if ($Header) { |
82 | | - $Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep |
83 | | - $Properties.Add($Header, $Clone) |
84 | | - } |
85 | | - # Create new property group |
86 | | - $Header = $Property.Substring(1, $Property.Length - 2) |
87 | | - $Sections.Clear() |
88 | | - } elseif ($Header -And $Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property -ne "") { |
89 | | - $Property = Read-Property -Property $Property |
90 | | - if ($Property.Count -gt 0) { |
91 | | - $Sections.Add($Property.Key, $Property.Value) |
92 | | - } else { |
93 | | - Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path" |
94 | | - } |
95 | | - } |
96 | | - } else { |
97 | | - # Ignore comments, sections, and blank lines |
98 | | - if ($Property[0] -ne "#" -And $Property[0] -ne ";" -And $Property[0] -ne "[" -And $Property -ne "") { |
99 | | - $Property = Read-Property -Property $Property |
100 | | - if ($Property.Count -gt 0) { |
101 | | - try { |
102 | | - $Properties.Add($Property.Key, $Property.Value) |
103 | | - } catch { |
104 | | - Write-Log -Type "WARN" -Object "Two distinct definitions of the property $($Property.Key) have been found in the configuration file" |
105 | | - $Errors += 1 |
106 | | - } |
107 | | - } else { |
108 | | - Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path" |
109 | | - } |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - } else { |
114 | | - # Alert that configuration file does not exist at specified location |
115 | | - Write-Log -Type "ERROR" -Message "Path not found $Path" -ExitCode 1 |
116 | | - } |
117 | | - if ($Errors -gt 0) { |
118 | | - Write-Log -Type "ERROR" -Object "Unable to proceed. Resolve the issues in $Path" -ExitCode 1 |
119 | | - } |
120 | | - return $Properties |
121 | | - } |
| 21 | + .EXAMPLE |
| 22 | + Read-Properties -Path ".\conf\default.ini" -Section |
| 23 | +
|
| 24 | + In this example, Read-Properties will parse the default.ini file contained in the .\conf directory and generate an ordered hashtable containing the key-values pairs. |
| 25 | +
|
| 26 | + .NOTES |
| 27 | + File name: Read-Properties.ps1 |
| 28 | + Author: Florian Carrier |
| 29 | + Creation date: 2018-11-27 |
| 30 | + Last modified: 2024-09-13 |
| 31 | + #> |
| 32 | + [CmdletBinding ()] |
| 33 | + Param ( |
| 34 | + [Parameter ( |
| 35 | + Position = 1, |
| 36 | + Mandatory = $true, |
| 37 | + HelpMessage = "Path to the property file" |
| 38 | + )] |
| 39 | + [ValidateNotNullOrEmpty ()] |
| 40 | + [String] |
| 41 | + $Path, |
| 42 | + [Parameter ( |
| 43 | + Position = 3, |
| 44 | + Mandatory = $false, |
| 45 | + HelpMessage = "Define if section headers should be used to group properties or be ignored" |
| 46 | + )] |
| 47 | + [Switch] |
| 48 | + $Section, |
| 49 | + [Parameter ( |
| 50 | + HelpMessage = "Switch to retrieve metadata about properties" |
| 51 | + )] |
| 52 | + [Switch] |
| 53 | + $Metadata |
| 54 | + ) |
| 55 | + Begin { |
| 56 | + # Get global preference variables |
| 57 | + Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState |
| 58 | + } |
| 59 | + Process { |
| 60 | + # Check that the file exists |
| 61 | + if (Test-Path -Path $Path) { |
| 62 | + # Load property file content |
| 63 | + $Content = Get-Content -Path $Path |
| 64 | + # Instantiate variables |
| 65 | + $Properties = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary" |
| 66 | + $Sections = New-Object -TypeName "System.Collections.Specialized.OrderedDictionary" |
| 67 | + $Errors = 0 |
| 68 | + $LineNumber = 0 |
| 69 | + $Header = $null |
| 70 | + $PreviousLine = $null |
| 71 | + # Read content line by line |
| 72 | + foreach ($Line in $Content) { |
| 73 | + $LineNumber += 1 |
| 74 | + # If properties have to be grouped by section |
| 75 | + if ($Section) { |
| 76 | + # If end of file and section is open |
| 77 | + if ($LineNumber -eq $Content.Count -And $Header) { |
| 78 | + if ($Line[0] -ne "#" -And $Line[0] -ne ";" -And $Line -ne "") { |
| 79 | + $Property = Read-Property -Property $Line |
| 80 | + if ($Property.Count -gt 0) { |
| 81 | + $Sections.Add($Property.Key, $Property.Value) |
| 82 | + } else { |
| 83 | + Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path" |
| 84 | + } |
| 85 | + } |
| 86 | + $Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep |
| 87 | + $Properties.Add($Header, $Clone) |
| 88 | + } elseif ($Line[0] -eq "[") { |
| 89 | + # If previous section exists add it to the property list |
| 90 | + if ($Header) { |
| 91 | + $Clone = Copy-OrderedHashtable -Hashtable $Sections -Deep |
| 92 | + $Properties.Add($Header, $Clone) |
| 93 | + } |
| 94 | + # Create new property group |
| 95 | + $Header = $Line.Substring(1, $Line.Length - 2) |
| 96 | + $Sections.Clear() |
| 97 | + } elseif ($Header -And $Line[0] -ne "#" -And $Line[0] -ne ";" -And $Line -ne "") { |
| 98 | + $Property = Read-Property -Property $Line |
| 99 | + if ($Property.Count -gt 0) { |
| 100 | + $Sections.Add($Property.Key, $Property.Value) |
| 101 | + } else { |
| 102 | + Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path" |
| 103 | + } |
| 104 | + } |
| 105 | + } else { |
| 106 | + # Parse rows |
| 107 | + if ($null -eq $Line -or $Line -eq "") { |
| 108 | + # Ignore empty lines |
| 109 | + } elseif ($Line[0] -eq "[") { |
| 110 | + # Parse sections |
| 111 | + $Header = $Line.Substring(1, $Line.Length - 2).Trim() |
| 112 | + } elseif ($Line[0] -eq "#" -Or $Line[0] -eq ";" ) { |
| 113 | + # Parse comments |
| 114 | + $PreviousLine = $Line.Substring(1, $Line.Length - 1).Trim() |
| 115 | + } else { |
| 116 | + # Parse properties |
| 117 | + $Property = Read-Property -Property $Line |
| 118 | + if ($Property.Count -gt 0) { |
| 119 | + if ($Metadata -eq $true) { |
| 120 | + # Create custom object including metadata |
| 121 | + $Value = [Ordered]@{ |
| 122 | + "Value" = $Property.Value |
| 123 | + "Description" = $PreviousLine |
| 124 | + "Section" = $Header |
| 125 | + } |
| 126 | + } else { |
| 127 | + # Return raw value |
| 128 | + $Value = $Property.Value |
| 129 | + } |
| 130 | + try { |
| 131 | + # Assign property |
| 132 | + $Properties.Add($Property.Key, $Value) |
| 133 | + } catch { |
| 134 | + Write-Log -Type "WARN" -Object "Two distinct definitions of the property $($Property.Key) have been found in the configuration file" |
| 135 | + $Errors += 1 |
| 136 | + } |
| 137 | + # Reset metadata |
| 138 | + $PreviousLine = $null |
| 139 | + } else { |
| 140 | + Write-Log -Type "WARN" -Message "Unable to process line $LineNumber from $Path" |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } else { |
| 146 | + # Alert that configuration file does not exist at specified location |
| 147 | + Write-Log -Type "ERROR" -Message "Path not found $Path" -ExitCode 1 |
| 148 | + } |
| 149 | + if ($Errors -gt 0) { |
| 150 | + Write-Log -Type "ERROR" -Object "Unable to proceed. Resolve the issues in $Path" -ExitCode 1 |
| 151 | + } |
| 152 | + } |
| 153 | + End { |
| 154 | + # Return list of properties |
| 155 | + return $Properties |
| 156 | + } |
122 | 157 | } |
0 commit comments