This project is read-only.

Get-Credential wasn't working after ps2exe

Feb 23, 2015 at 2:40 PM
I was using Get-Credential in my ps1 code and the created .exe was not working as expected. I was being prompted in the console for a username, but it would fail saying the password was null and not allow me to type in my password. I fixed this by updating the PromptForCredential function, specifically the one below, removing the if-statement.
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
    if (!CONSOLE)
    {
        ik.PowerShell.CredentialForm.UserPwd cred = CredentialForm.PromptForPassword(caption, message, targetName, userName, allowedCredentialTypes, options);
        if (cred != null )
        {
            System.Security.SecureString x = new System.Security.SecureString();
            foreach (char c in cred.Password.ToCharArray())
                x.AppendChar(c);

            return new PSCredential(cred.User, x);
        }
        return null;
    }
        
    if (!string.IsNullOrEmpty(caption))
        WriteLine(caption);
    WriteLine(message);
    Write("User name: ");
    string un = ReadLine();
    SecureString pwd = null;
    Write("Password: ");
    pwd = ReadLineAsSecureString();
    PSCredential c2 = new PSCredential(un, pwd);
    return c2;
}
Once the above was fixed, I noticed that the password was being displayed in plain text, something that would not be acceptable for the .exe I was creating. To fix this, I updated the function ReadLineAsSecureString(), see below for code update.
public override System.Security.SecureString ReadLineAsSecureString()
{
    System.Security.SecureString x = new System.Security.SecureString();
    while (true)
    {
        ConsoleKeyInfo i = Console.ReadKey(true);
        if (i.Key == ConsoleKey.Enter)
        {
            break;
        }
        else if (i.Key == ConsoleKey.Backspace)
        {
            if (x.Length > 0)
            {
                x.RemoveAt(x.Length - 1);
                Console.Write("\b \b");
            }
        }
        else
        {
            x.AppendChar(i.KeyChar);
            Console.Write("*");
        }
    }
    return x;
}
This works for the console application, but when using -noconsole, we are unable to use Get-Credential at all, as there is no prompt for credentials. Can I recommend the above code be added to the repository as an enhancement? Is there any troubleshooting or code updates to allow Get-Credential to work when -noconsole is used? Thank you.
Mar 5, 2015 at 1:14 PM
Do you want to send me the complete ps2exe-script with your modifications? Don't forget to add a comment in the top with your name.
Mar 9, 2015 at 9:52 PM
I can do that. What is your preferred method of delivery?
Jun 27, 2016 at 11:23 AM
I have the same problem. I solved this issue.

Code not Work:
$cred = Get-credential

Code Works:
$Cred = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "")