Functions/Authentication/PSCredential/MCM/Rename-MCMCredential.ps1
<#
.DESCRIPTION This script retrieves secured credentials from the Microsoft Credential Manager and then recreates them with a different username. This is for use in distributing connectors that leverage differing usernames for the same underlying identity (especially regarding the presence or absence of a domain with the username) The target, username, and hint bindings are maintained in the MCI Memspace (via Import-MCMCredentialInfo) #> function Rename-MCMCredential { [CmdletBinding()] Param ( [Parameter(Mandatory=$false,ParameterSetName='NoDomain')] [switch] $NoDomain, [Parameter(Mandatory=$false,ParameterSetName='Overwrite')] [string] $Overwrite ) 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 = Test-MCMCredential -Target $Target if ($NoDomain) { $UserName = if($MCMCred.username -like "*\*"){($MCMCred.username -split '\\')[1]}else{$MCMCred.username} $ReCred = New-Object pscredential ($UserName,$MCMCred.password) } if ($Overwrite) { $UserName = $Overwrite $ReCred = New-Object pscredential ($UserName,$MCMCred.password) } if ($ReCred){$ReCred} } } |