pf-CmdKey.ps1
function Set-CmdKeyCredentials { param ( $username, $password, $credential, [Parameter(ValueFromPipeline=$true)] $target ) begin { if ($credential) { $password = $credential.GetNetworkCredential().Password $username = $credential.UserName } } process { if ( $target -notlike '*:*' ) { cmdkey /add:$target /user:$username /pass:$password } else{ cmdkey /generic:LegacyGeneric:target=$target /user:$username /pass:$password } } } function Set-CmdKeyCredentials:::Example { 'clbhvvhwapp0' | Set-CmdKeyCredentials -username 'CorpDomain\AppUser2' -password CorpDomain2015 Get-CmdKey | Where-Object User -Like CorpDomain* } function Get-CmdKey { $lines = Invoke-Exe cmdkey /list -OkExitCode 0, 1 | ForEach-Object { $_.Trim() } # $lines = $output | skip-until { # $_ -like '*Currently stored credentials*' # } -exclude $entries = $lines | Get-ListRecords -fieldSeparator ':' foreach ($entry in $entries) { if (-not $entry.Target) { continue } $targetParts = $entry.Target | Get-HeadAndTail -fieldSeparator ':' $targetType = $targetParts.Head $targetValue = $targetParts.Tail | Update-Prefix 'target=' $entry | Add-Object_PropertyMember -Name 'targetType' -Value $targetType | Add-Object_PropertyMember -Name 'targetValue' -Value $targetValue } } function Clear-CmdKey { $entries = Get-CmdKey $entries.target | ForEach-Object { Invoke-Exe cmdkey /delete $_ -OkExitCode 0, 1 } } |