Functions/Authentication/PSCredential/Get-CachedCredential.ps1

<#
.DESCRIPTION
    This script is meant to be called primarily through the Test-CachedCredential Function
    It retrieves secured credentials from the selected Credentialing Cache (Microsoft Credential Manager or Encrypted SecureString Files)
    The Metadata for preset credentials is maintained in $Global:PS_CredentialMetadata (via Import/Export-CredentialMetadata)
 
#>

function Get-CachedCredential
{    
    [CmdletBinding()]
    Param()
    DynamicParam 
    {
        # Define Metadata Locally
        $Metadata = [array]($Global:PS_CredentialMetadata)

        # Dynamic Parameter for Credential Targets/Names
        $RuntimeParameterDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
        $RuntimeParameterDictionary.Add("Name", (New-DynamicParameter -ParamName "Name" -ValueType string -DataSet $Metadata.name -Mandatory:$true))
        $RuntimeParameterDictionary.Add("Cache", (New-DynamicParameter -ParamName "Cache" -ValueType string -DataSet @("MCM","ESS") -Mandatory:$false -DefaultValue $Global:PS_CredentialCacheType))
        return $RuntimeParameterDictionary   
    }
    
    Begin
    {
        # Convert Runtime Parameter Dictionary into Available Constants
        foreach ($key in $RuntimeParameterDictionary.keys){New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value}
        $CM = $Metadata | where name -eq $Name
    }

    Process
    {
        # Switch Retrieval Function Based on Cache
        $Cred = switch ($Cache)
        {
            "MCM" {try{Get-MCMCredential -Name $Name}catch{$null}}
            "ESS" {try{Get-ESSCredential -Name $Name}catch{$null}}
        }
        if($CRED){$CRED}
    }
}