Functions/Set-SecretEnvironmentVariable.ps1

<#
.SYNOPSIS
Sets Environment Variables based on rows in CSV.
 
.DESCRIPTION
Takes Secret Scopes from config, finds correspinding $csv files and sets $env:vars to be used for Set-DatabricksSecrets.
 
Main config entry used is "secretsCsvPath"
 
This is the folder location of where local secrets csvs are, not the actual file itself.
A secrets folder can be named with the secret scope as a prefix
For example, if a config has these scopes -
 
    "secretScopes":["scopeOne","scopeTwo"]
 
    then two folders - "scopeOne/scopeOne_secrets.csv" and "scopeTwo/scopeTwo_secrets.csv" are required.
 
    Note that the csv file names do not need to be named the same, but for consistencies sake limit it to one file with a sensible name
 
.PARAMETER config
config passed in that includes secret scopes and secret scopes csv
 
.EXAMPLE
Set-DatabricksSecrets -BearerToken $bearerToken -config $config
 
.NOTES
Author: Sabin IO
 
#>

Function Set-SecretEnvironmentVariable {  
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)][psobject]$config
    )
    Write-Verbose "Setting secrets in csv as environment variables..."
    foreach ($secretScope in $config.secretScopes) {
        Write-Verbose "Finding all csv file with prefix $secretScope"
        $secretCsvFolderPath = Join-Path $config.secretsCsvPath $secretScope
        $secretCsvFilePath = Get-ChildItem $secretCsvFolderPath
        Write-Verbose "csv folder path:" $secretCsvFolderPath
        Write-Verbose "csv file path:" $secretCsvFilePath
        $databricksSecrets = Import-Csv -Path $secretCsvFilePath -Header "Name", "Value" | Select-Object -Skip 1
        foreach ($databricksSecret in $databricksSecrets) {
            if (($databricksSecret.Name.StartsWith($secretScope))-eq $false) {
                $databricksSecret.Name = $secretScope+$databricksSecret.Name 
            }
            Write-Host "creating envvar $($databricksSecret.Name)"
            Set-Item "env:$($databricksSecret.Name)" $databricksSecret.Value
        }
    }
}