Functions/Authentication/PSCredential/MCM/Test-MCMCredential.ps1
<#
.DESCRIPTION This script is meant to be called primarily through the Test-MCMCredential Script It either creates or retrieves secured credentials that are in the Microsoft Credential Manager It is intended to be called by other modules for specific credentialing as part of their import process. It leverages the Get-MCMCredential and New-MCMCredential functions to operate. #> function Test-MCMCredential { [CmdletBinding()] Param() DynamicParam { # Get MCM Credential Info $MCMInfo = Import-MCMCredentialInfo # Instantiate Runtime Parameter Dictionary $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 { # Check for Existing Credential in the Microsoft Credential Manager $MCMCredential = Get-MCMCredential -Target $Target if (!$MCMCredential) { # Create new MCM Credential if one doesn't already exist New-MCMCredential -Target $Target $MCMCredential = Get-MCMCredential -Target $Target } # Check for creation and then output if($MCMCredential){$MCMCredential} else{Throw "Could not retrieve/create new MCM Credential for Target: $Target"} } } |