Private/Get-INISectionValues.ps1

<#
.SYNOPSIS
Parses an INI configuration file and returns key-value pairs for a given section or a specific key as an array.
 
.DESCRIPTION
The Get-INISectionValues function reads a `.ini` file and retrieves all key-value pairs from a specified section.
If a `Key` is also provided, only the value for that key is returned (as an array if the value is comma-separated).
If the `IniPath` parameter is not provided, the function defaults to the config.ini located in the `PSCitrixPowerBi` module directory.
 
.PARAMETER IniPath
The full path to the INI file. If not supplied, it defaults to the config.ini within the `PSCitrixPowerBi` module.
 
.PARAMETER Section
The section name in the INI file (e.g., `[prod]`) from which to extract values.
 
.PARAMETER Key
(Optional) The specific key within the section to retrieve. The result is returned as an array by default.
 
.EXAMPLE
Get-INISectionValues -Section "prod"
 
Returns all key-value pairs under the [prod] section in the config.ini.
 
.EXAMPLE
Get-INISectionValues -Section "prod" -Key "DeliveryControllers"
 
Returns the value of the DeliveryControllers key under [prod] section as an array.
 
.EXAMPLE
Get-INISectionValues -IniPath "C:\Config\custom.ini" -Section "Excel" -Key "ExcelPath"
 
Returns the path configured in the [Excel] section from a custom INI file.
 
.NOTES
- Keys with comma-separated values are automatically returned as arrays.
- If the key does not exist, a warning is shown and an empty array is returned.
- Comments (`#`) and blank lines are ignored during parsing.
- The function assumes standard INI format: [Section] followed by key=value lines.
#>


function Get-INISectionValues {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)]
        [string]$IniPath,

        [Parameter(Mandatory)]
        [string]$Section,

        [Parameter()]
        [string]$Key
    )

    # Default to config.ini from the module folder if not provided
    if (-not $IniPath) {
        $module = Get-Module -Name "PSCitrixPowerBi" -ListAvailable | Select-Object -First 1
        if ($null -eq $module) {
            Write-Error "❌ Module 'PSCitrixPowerBi' not found."
            return
        }
        $IniPath = Join-Path -Path $module.ModuleBase -ChildPath "Public\Config\Config.ini"
    }

    if (-not (Test-Path $IniPath)) {
        Write-Error "❌ INI file not found at: $IniPath"
        return
    }

    try {
        $iniContent = Get-Content $IniPath | Where-Object { $_ -notmatch '^\s*#' -and $_ -match '\S' }
        $currentSection = ''
        $sectionValues = @{}

        foreach ($line in $iniContent) {
            if ($line -match '^\[(.+)\]$') {
                $currentSection = $matches[1].Trim()
            } elseif ($currentSection -eq $Section -and $line -match '^\s*(.+?)\s*=\s*(.*)$') {
                $k = $matches[1].Trim()
                $v = $matches[2].Trim()
                $sectionValues[$k] = $v
            }
        }

        if ($Key) {
            if ($sectionValues.ContainsKey($Key)) {
                return ($sectionValues[$Key] -split '\s*,\s*')  # Return as array
            } else {
                Write-Warning "⚠️ Key [$Key] not found in section [$Section]."
                return @()
            }
        }

        if ($sectionValues.Count -eq 0) {
            Write-Warning "⚠️ No values found under section [$Section] in $IniPath"
        }

        return $sectionValues
    }
    catch {
        Write-Error "❌ Failed to read INI file: $_"
    }
}