Functions/Authentication/Import-CredentialMetadata.ps1

<#
.DESCRIPTION
    This script imports a JSON configuration defining metadata for stored credentials.
    This enables secure and unattended retrieval of credentials by automatic processes.
#>

function Import-CredentialMetadata
{
    [CmdletBinding()]
    Param
    (
        # Filename of Metadata
        [Parameter(Mandatory=$false)]
        [String]
        $Filename = "CredMeta",

        # Path to place JSON metadata file (Should be defined in PS Profile)
        [Parameter(Mandatory=$false)]
        [String]
        $CredentialPath = $Global:PS_CredentialPath,

        # User Context Object (Should be defined in PS Profile)
        [Parameter(Mandatory=$false)]
        [psobject]
        $Context = $Global:PS_UserContext,

        # Infrastructural Environment (Core or OFE)
        [Parameter(Mandatory=$false)]
        [String]
        $EnvName = $Global:PS_EnvName
    )

    Process
    {
        # Define Metadata Path and test for existing
        $MetaPath = "$CredentialPath\$filename.json"
        $CredMetaTest = Test-Path $MetaPath
        if(!$CredMetaTest) {Export-CredentialMetadata -Filename $Filename -CredentialPath $CredentialPath -EnvName $EnvName -Context $Context}
        
        # Retrieve Metadata
        Get-Content $MetaPath | ConvertFrom-Json
    }
}