Asking for Credentials

by Dec 12, 2011

When you write functions that accept credentials as parameters, add a transformation attribute! This way, the user can either submit a credential object (for example, a credential supplied from Get-Credential), or simply a user name as string. The transformation attribute will then convert this string automagically into a credential and ask for a password.

Here is a sample function:

function do-something  { 
param(
  [System.Management.Automation.Credential()]
  $Credential
)
'$Credential now holds a valid credential object'
$Credential
}

When you run do-something without parameters, it will automatically invoke the credentials dialog.

Twitter This Tip!
ReTweet this Tip!