I got an exception when creating a credential using a domanname\user
This is what worked for me
function setCredential {
param(
[parameter(mandatory=$true,position=0)]
[string] $keyName,
[parameter(mandatory=$true,position=1,parametersetname="UsernamePassword")]
[string] $username,
[parameter(mandatory=$true,position=2,parametersetname="UsernamePassword")]
[string] $password,
[parameter(mandatory=$true,position=1,parametersetname="PSCredential")]
[System.Management.Automation.PSCredential] $credential,
$cert,
$store = (gi .)
)
if( $cert -eq $null ) {
$cert = selectCertificate
}
if( $credential -ne $null ) {
$networkcredential = $credential.GetNetworkCredential()
$username = $credential.Username
$password = $networkcredential.Password
}
if( !($username -and $password) ) {
throw "Must specify credential or non-empty username+password"
return
}
if( $cert.GetType().Name -eq "String" ) {
$cert = ls cert:\CurrentUser\My | ?{ $_.Subject -match $cert } | select -first 1
}
sc -Encoding Ascii -Path (keyFilePath $store $keyName) -Value @( $cert.Thumbprint, (PKCSEncrypt "${username}:${password}" $cert) )
$TestCred = getCredential -keyName $keyName -store $store
$TestNetcred = $TestCred.GetNetworkCredential()
if( $TestCred.Username -ne $username -or $TestNetcred.Password -ne $password ) {
throw "Failed to encrypt credential"
}
}
The problem is the NetworkCredential puts the domain in a different Field. You need to get the username from the PSCredential and the password from the NetworkCredential
Sorry... never used git so I didn't contribute. LOL. great code otherwise