Functions/Authentication/PSCredential/New-CachedCredential.ps1
<#
.DESCRIPTION This script is meant to be called primarily through the Test-CachedCredential Function It creates 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 New-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)) $RuntimeParameterDictionary.Add("Credential", (New-StaticParameter -ParamName "Credential" -ValueType pscredential -Mandatory:$false)) $RuntimeParameterDictionary.Add("CredentialPath", (New-StaticParameter -ParamName "CredentialPath" -ValueType string -Mandatory:$false -DefaultValue $Global:PS_CredentialPath)) 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 switch ($Cache) { "MCM" {try{if($Credential){New-MCMCredential -Name $Name -Credential $Credential}else{New-MCMCredential -Name $Name}}catch{$null}} "ESS" {try{if($Credential){New-ESSCredential -Name $Name -Credential $Credential -CredentialPath $CredentialPath}else{New-ESSCredential -Name $Name -CredentialPath $CredentialPath}}catch{$null}} } } } |