Credentials in the Console

A while back on the Windows PowerShell Team blog, there was a post that describes how to force Get-Credential to prompt for a username and password in the console itself rather than popping up a Windows dialog box asking for a username and password.

The change is a key in the registry, and is permanent, unless of course you change it back to the original setting. The other minor complaint was that the there was no space between where the user needs to type a username and password, and the text prompting for the text.

   1: 119 >  $c = Get-Credential
   2:  
   3: cmdlet Get-Credential at command pipeline position 1
   4: Supply values for the following parameters:
   5: Credential
   6: PromptForCredential_UserAndy
   7: PromptForCredential_Password********
   8:  
   9: 120 >

You can see that the "PromptForCredential" and my username - "Andy" just run together. 

You can accomplish this another way with much more control over the user experience and you don't have to hack the registry.

So here is my quick and dirty function. I put in a very basic check to see if someone added their domain or not, and added it if necessary.

   1: function Get-Cred {
   2:     Write-Host "";
   3:     $username = Read-Host "Enter username to access some resource (no domain required)"
   4:     if ($username -notlike "MYDOMAIN\*"){$username = "MYDOMAIN\$username"}
   5:         
   6:     Write-Host ""
   7:     $password = Read-Host  -AsSecureString "Password to access some resource"
   8:     
   9:     $credential = New-Object System.Management.Automation.PSCredential($username,$password)
  10:     return $credential
  11: }

And here it is in action:

   1: 130 >  $cred = Get-Cred
   2:  
   3: Enter username to access some resource (no domain required): andy
   4:  
   5: Password to access some resource: **************
   6: 131 >
   7: 131 >  $cred.GetNetworkCredential() | fl *
   8:  
   9:  
  10: UserName : andy
  11: Password : secretpassword
  12: Domain   : MYDOMAIN

Comments (2) -

Sorry, I don't understand. I can see what the function "get-creds" does, but I just can't figure out what you mean by "And here it is in action".
Did you mean to enter those commands in the PoSh?
I'd be very pleased with some more hints on that.
Thanks Wink
G.

By in action i just meant to show what the user experience is when you call the function.

I also called the GetNetworkCredentials() method to show that it was indeed a PSCredential object just like Get- Credential creates.

Hope that helps

Andy

Comments are closed