Credentials in the Console
July 23, 2008 at 9:07 PM
—
Andy Schneider
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
a1792254-b881-41a9-b515-b20fbd7babb4|0|.0
Posted in:
Tags: