Prompting user for username and password
When the cmdLet is inovoked …
PS C:\> $cred = get-credentail
… it prompts the user for the username and password:
In order to get a
PSCredential
object without a (what I perceive to be intrusive) message Box, the method
promptForCredential
of
$host.ui
can be used:
$cred = $host.ui.promptForCredential('Connecting to XYZ', 'Enter username and password.', '', 'XYZ')
Connecting to XYZ
Enter username and password.
User: rene
Password for user rene: ********
Getting a PSCredential object without user-interaction
If the username and password are already stored in
variables, it is possible to create a
psCredential
object without user interaction:
$username = 'rene'
$password = 'secret!'
$secStr = convertTo-secureString -string $password -asPlainText -force
$cred = new-object System.Management.Automation.PSCredential $username, $secStr