Functions/Get-Settings.ps1

Function Get-Settings {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification='Not changed yet')]
    
    param(
        [parameter(Mandatory=$true)]
        [string]$configRootPath,
        [string]$environment,
        [parameter(ValueFromRemainingArguments = $true)]
        $overrides
    )
 
    $basePath = join-path $configRootPath (GetBaseConfigPath) 
    
    $basePath = GetFirstConfigFile $basePath

    $settings = [PSCustomObject]@{}
    Import-SettingsFromFile $settings $basePath 

    if ($environment){
        $envPath = join-path $configRootPath (GetEnvironmentConfigPath -environment $environment)

        $envPath = GetFirstConfigFile $envPath
        Write-Verbose "Loading config from $environment"    
        Import-SettingsFromFile $settings $envPath 
    }
    
    Import-SettingsFromPersonalFile -ConfigRootPath $configRootPath -Settings $settings -environment $environment
    
    #Loop through environment variables and set values
    foreach ($envSetting in (Get-ChildItem env:)){
        $SettingName =$envSetting.name
        $dotpos = $SettingName.indexof(".")
        if ($dotpos -gt 0) {
            $SettingName= $SettingName.substring(0,$dotpos)
        }
        if ($null -ne $settings.$SettingName) {
            Write-Verbose "Setting $($_.Name) to $((get-item env:$($_.Name)).value) from an override parameter passed in"
            Set-SettingsValue $settings -name $envSetting.name -value $envSetting.value
        }
    }

    if ($settings.keyVaultConfigs) {
        $settings.keyVaultConfigs | Foreach-Object {
            $keyVault = $_

            (Get-KeyVaultSecrets -vaultName $keyVault) | ForEach-Object {    
                $property = $_

                $settings | Add-Member -MemberType NoteProperty -Name  $property.Name -Value $property.Value -Force
    
                Write-Verbose "Adding $($property.Name) -Value $($property.Value) from keyvault: $keyVault"
            }
        }
    }

    if (-not $null -eq $overrides) {
        Write-Host "Loading settings from overrides"

        #Overrides is an array of the remaining arguments passed through.
        #if the following had been passed -param1 value -param2 value2
        #then you would have a 4 item array i.e. @("-param1","value","-param2","value2")

        for ($i = 0; $i -lt $overrides.Count; $i++) {
            $name = $overrides[$i].trim('-')
            if ($name[-1] -eq ":"){$name = $name.substring(0,$name.Length -1)}
                
            #Switch parameters are either the last param or the next item is another param and starts with a -
            if ($i -eq $overrides.count - 1 -or $overrides[$i + 1] -like "-*") {
                $value = $true
            }
            else {
                $value = $overrides[$i + 1]
                $i++
            }
            Write-Verbose "Setting $name to $value from an override parameter passed in"
            Set-SettingsValue $settings -value $value -name $name 
        }
    }


    $settings = Invoke-SettingEvaluation -settings $settings
    $settings
}