AlvinSeville7cf

IniParser

Jul 21st, 2021 (edited)
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Get-IniItem displays found key-value pairs in ini file.
  4.  
  5. .Description
  6. Get-IniItem displays found key-value pairs in ini file based on passed wildcard expression.
  7. Only *, ?, [] wildcards are supported.
  8.  
  9. .PARAMETER Name
  10. Specifies ini file path.
  11.  
  12. .PARAMETER Filter
  13. Specifies key-value pair file filter. Use section.key syntax to filter key-value pairs. If key-value pair has no section you can omit section name.
  14.  
  15. .INPUTS
  16. No inputs accepted.
  17.  
  18. .OUTPUTS
  19. System.Collections.Generic.Dictionary<string, string>
  20. #>
  21.  
  22. using namespace System.Collections.Generic;
  23.  
  24. [CmdletBinding()]
  25. param (
  26.     [switch]
  27.     $Version,
  28.     [Parameter(Mandatory)]
  29.     [string]
  30.     $Name,
  31.     [Parameter(Mandatory)]
  32.     [string]
  33.     $Filter
  34. )
  35.  
  36. class Section {
  37.     [string] $Name;
  38.     [IDictionary[string, string]] $KeyValuePairs
  39.  
  40.     Section([string] $name) {
  41.         $this.Name = $name
  42.         $this.KeyValuePairs = New-Object -TypeName "Dictionary[string, string]"
  43.     }
  44.  
  45.     [void] Add([string] $key, [string] $value) {
  46.         if (!$key) {
  47.             throw "Key must be non-empty string."
  48.         }
  49.  
  50.         $this.KeyValuePairs.Add($key, $value)
  51.     }
  52.  
  53.     [void] Remove([string] $key) {
  54.         if (!$key) {
  55.             throw "Key must be non-empty string."
  56.         }
  57.  
  58.         $this.KeyValuePairs.Remove($key)
  59.     }
  60. }
  61.  
  62. function Get-IniFileContents($name) {
  63.     if (-not (Test-Path $name)) {
  64.         throw "File doesn't exist."
  65.     }
  66.  
  67.     $globalSection = New-Object -TypeName "Section" -ArgumentList ""
  68.     $currentSection = $globalSection
  69.     $sections = @($globalSection)
  70.  
  71.     foreach ($line in Get-Content $name) {
  72.         switch -Regex ($line) {
  73.             "^\s*(#.*)?$" {
  74.                 continue
  75.             }
  76.             "^\s*\[(.+?)\]\s*$" {
  77.                 $currentSection = New-Object -TypeName "Section" -ArgumentList $Matches[1]
  78.                 $sections += $currentSection
  79.             }
  80.             "^\s*(\w+)=(\w+)\s*$" {
  81.                 $currentSection.Add($Matches[1], $Matches[2])
  82.             }
  83.             default {
  84.                 throw "Wrong ini file format: `"$line`" must be an empty line, comment, section or key-value-pair declaration."
  85.             }
  86.         }
  87.     }
  88.  
  89.     Write-Output $sections
  90. }
  91.  
  92. Set-Variable -Name SuccessStatus -Value 0 -Option Constant
  93. Set-Variable -Name WrongOptionStatus -Value 2 -Option Constant
  94.  
  95. if ($Filter -notmatch "\.") {
  96.     $Filter = ".$Filter"
  97. }
  98.  
  99. $filterArray = $Filter -split "\."
  100. $sectionName = $filterArray[0]
  101. $keyName = $filterArray[1]
  102.  
  103. Get-IniFileContents $Name |
  104.     Where-Object { $_.Name -like $sectionName } |
  105.         Select-Object -ExpandProperty KeyValuePairs
  106.  
  107. exit $SuccessStatus
  108.  
Add Comment
Please, Sign In to add comment