Functions/Authentication/PSCredential/MCM/Get-MCMCredential.ps1

<#
.DESCRIPTION
    This script is meant to be called primarily through the Test-MCMCredential Script
    It retrieves secured credentials from the Microsoft Credential Manager
    The target, username, and hint bindings are maintained in the MCI Memspace (via Import-MCMCredentialInfo)
 
#>

function Get-MCMCredential
    {    
    [CmdletBinding()]
    Param()
    DynamicParam 
        {
        # Get MCM Credential Info
        $MCMInfo = Import-MCMCredentialInfo

        # Dynamic Parameter for MCI Targets
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $ParameterName = "Target"
        $RunTimeParameter = New-DynamicParameter -ParamName $ParameterName -ValueType string -DataSet $MCMInfo.Target -Mandatory:$true
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
        return $RuntimeParameterDictionary   
        }
    
    Begin
        {
        # Burn in parameter variables from Runtime Parameter Dictionary
        foreach ($key in $RuntimeParameterDictionary.keys){New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value}
        }

    Process
        {
        # Retrieve Credential from MCM
        $MCMCred = Get-StoredCredential -Target $Target
        if ($MCMCred){$MCMCred}
        }
    }