Created
January 21, 2017 16:28
-
-
Save quonic/64816cc8ee57c91e2332ee9c6386699e to your computer and use it in GitHub Desktop.
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
$myIniSettings = Get-IniContent mySettings.ini | |
$myIniSettings | ConvertTo-Json | |
<# Returns | |
{ | |
"second": { | |
"dotheneedful": "1234", | |
"donothing": "\"doing it\"" | |
}, | |
"first": { | |
"setting2": "lol what", | |
"setting1": "true" | |
} | |
} | |
#> | |
$myIniSettings.first.setting1 | |
# Returns: true | |
# https://blogs.technet.microsoft.com/heyscriptingguy/2011/08/20/use-powershell-to-work-with-any-ini-file/ | |
function Get-IniContent ($filePath) | |
{ | |
$ini = @{} | |
switch -regex -file $FilePath | |
{ | |
“^\[(.+)\]” # Section | |
{ | |
$section = $matches[1] | |
$ini[$section] = @{} | |
$CommentCount = 0 | |
} | |
“^(;.*)$” # Comment | |
{ | |
$value = $matches[1] | |
$CommentCount = $CommentCount + 1 | |
$name = “Comment” + $CommentCount | |
$ini[$section][$name] = $value | |
} | |
“(.+?)\s*=(.*)” # Key | |
{ | |
$name,$value = $matches[1..2] | |
$ini[$section][$name] = $value | |
} | |
} | |
return $ini | |
} |
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
[first] | |
setting1=true | |
setting2=lol what | |
[second] | |
dotheneedful=1234 | |
donothing="doing it" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment