-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Separated functions into individual files
* Added Unit Tests (not tested yet) * Small optimizations in code and naming * cleanup
- Loading branch information
Showing
10 changed files
with
451 additions
and
394 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
*.psd1 text | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
Function Get-IniContent { | ||
<# | ||
.Synopsis | ||
Gets the content of an INI file | ||
.Description | ||
Gets the content of an INI file and returns it as a hashtable | ||
.Notes | ||
Author : Oliver Lipkau <oliver@lipkau.net> | ||
Source : https://github.com/lipkau/PsIni | ||
http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91 | ||
Version : 1.0.0 - 2010/03/12 - OL - Initial release | ||
1.0.1 - 2014/12/11 - OL - Typo (Thx SLDR) | ||
Typo (Thx Dave Stiff) | ||
1.0.2 - 2015/06/06 - OL - Improvment to switch (Thx Tallandtree) | ||
1.0.3 - 2015/06/18 - OL - Migrate to semantic versioning (GitHub issue#4) | ||
1.0.4 - 2015/06/18 - OL - Remove check for .ini extension (GitHub Issue#6) | ||
1.1.0 - 2015/07/14 - CB - Improve round-tripping and be a bit more liberal (GitHub Pull #7) | ||
OL - Small Improvments and cleanup | ||
#Requires -Version 2.0 | ||
.Inputs | ||
System.String | ||
.Outputs | ||
System.Collections.Hashtable | ||
.Parameter FilePath | ||
Specifies the path to the input file. | ||
.Parameter HashComments | ||
Treat lines starting with # as a comment. | ||
.Parameter StripComments | ||
Remove lines determined to be comments from the resulting dictionary. | ||
.Example | ||
$FileContent = Get-IniContent "C:\myinifile.ini" | ||
----------- | ||
Description | ||
Saves the content of the c:\myinifile.ini in a hashtable called $FileContent | ||
.Example | ||
$inifilepath | $FileContent = Get-IniContent | ||
----------- | ||
Description | ||
Gets the content of the ini file passed through the pipe into a hashtable called $FileContent | ||
.Example | ||
C:\PS>$FileContent = Get-IniContent "c:\settings.ini" | ||
C:\PS>$FileContent["Section"]["Key"] | ||
----------- | ||
Description | ||
Returns the key "Key" of the section "Section" from the C:\settings.ini file | ||
.Link | ||
Out-IniFile | ||
#> | ||
|
||
[CmdletBinding()] | ||
Param( | ||
[ValidateNotNullOrEmpty()] | ||
[ValidateScript({(Test-Path $_)})] | ||
[Parameter(ValueFromPipeline=$True,Mandatory=$True)] | ||
[string]$FilePath, | ||
[char[]]$CommentChar = @(";"), | ||
[switch]$StripComments | ||
) | ||
|
||
Begin | ||
{ | ||
Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started" | ||
$commentRegex = "^([$($CommentChar -join '')].*)$" | ||
} | ||
|
||
Process | ||
{ | ||
Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath" | ||
|
||
$ini = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase) | ||
switch -regex -file $FilePath | ||
{ | ||
"^\[(.+)\]$" # Section | ||
{ | ||
$section = $matches[1] | ||
$ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase) | ||
$CommentCount = 0 | ||
continue | ||
} | ||
$commentRegex # Comment | ||
{ | ||
if (!$StripComments) | ||
{ | ||
if (!($section)) | ||
{ | ||
$section = "_" | ||
$ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase) | ||
} | ||
$value = $matches[1] | ||
$CommentCount++ | ||
$name = "Comment" + $CommentCount | ||
$ini[$section][$name] = $value | ||
} | ||
continue | ||
} | ||
"(.+?)\s*=\s*(.*)" # Key | ||
{ | ||
if (!($section)) | ||
{ | ||
$section = "_" | ||
$ini[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase) | ||
} | ||
$name,$value = $matches[1..2] | ||
$ini[$section][$name] = $value | ||
continue | ||
} | ||
} | ||
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath" | ||
Return $ini | ||
} | ||
|
||
End | ||
{Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"} | ||
} |
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
Oops, something went wrong.