1+ # helper functions
2+
3+ Function Get-IniContent {
4+ <#
5+ . Synopsis
6+ Gets the content of an INI file
7+
8+ . Description
9+ Gets the content of an INI file and returns it as a hashtable
10+
11+ . Notes
12+ Author : Oliver Lipkau <oliver@lipkau.net>
13+ Blog : http://oliver.lipkau.net/blog/
14+ Source : https://github.com/lipkau/PsIni
15+ http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
16+ Version : 1.0 - 2010/03/12 - Initial release
17+ 1.1 - 2014/12/11 - Typo (Thx SLDR)
18+ Typo (Thx Dave Stiff)
19+
20+ #Requires -Version 2.0
21+
22+ . Inputs
23+ System.String
24+
25+ . Outputs
26+ System.Collections.Hashtable
27+
28+ . Parameter FilePath
29+ Specifies the path to the input file.
30+
31+ . Example
32+ $FileContent = Get-IniContent "C:\myinifile.ini"
33+ -----------
34+ Description
35+ Saves the content of the c:\myinifile.ini in a hashtable called $FileContent
36+
37+ . Example
38+ $inifilepath | $FileContent = Get-IniContent
39+ -----------
40+ Description
41+ Gets the content of the ini file passed through the pipe into a hashtable called $FileContent
42+
43+ . Example
44+ C:\PS>$FileContent = Get-IniContent "c:\settings.ini"
45+ C:\PS>$FileContent["Section"]["Key"]
46+ -----------
47+ Description
48+ Returns the key "Key" of the section "Section" from the C:\settings.ini file
49+
50+ . Link
51+ Out-IniFile
52+ #>
53+
54+ [CmdletBinding ()]
55+ Param (
56+ [ValidateNotNullOrEmpty ()]
57+ [ValidateScript ({(Test-Path $_ ) -and ((Get-Item $_ ).Extension -eq " .ini" )})]
58+ [Parameter (ValueFromPipeline = $True , Mandatory = $True )]
59+ [string ]$FilePath
60+ )
61+
62+ Begin
63+ {Write-Verbose " $ ( $MyInvocation.MyCommand.Name ) :: Function started" }
64+
65+ Process
66+ {
67+ Write-Verbose " $ ( $MyInvocation.MyCommand.Name ) :: Processing file: $Filepath "
68+
69+ $ini = @ {}
70+ switch - regex - file $FilePath
71+ {
72+ " ^\[(.+)\]$" # Section
73+ {
74+ $section = $matches [1 ]
75+ $ini [$section ] = @ {}
76+ $CommentCount = 0
77+ }
78+ " ^(;.*)$" # Comment
79+ {
80+ if (! ($section ))
81+ {
82+ $section = " No-Section"
83+ $ini [$section ] = @ {}
84+ }
85+ $value = $matches [1 ]
86+ $CommentCount = $CommentCount + 1
87+ $name = " Comment" + $CommentCount
88+ $ini [$section ][$name ] = $value
89+ }
90+ " (.+?)\s*=\s*(.*)" # Key
91+ {
92+ if (! ($section ))
93+ {
94+ $section = " No-Section"
95+ $ini [$section ] = @ {}
96+ }
97+ $name , $value = $matches [1 .. 2 ]
98+ $ini [$section ][$name ] = $value
99+ }
100+ }
101+ Write-Verbose " $ ( $MyInvocation.MyCommand.Name ) :: Finished Processing file: $FilePath "
102+ Return $ini
103+ }
104+
105+ End
106+ {Write-Verbose " $ ( $MyInvocation.MyCommand.Name ) :: Function ended" }
107+ }
0 commit comments