Home Dashboard Directory Help
Search

Specifying a secure string on the command line by Shay Levi


Status: 

Active


4
1
Sign in
to vote
Type: Suggestion
ID: 773183
Opened: 12/1/2012 2:36:43 AM
Access Restriction: Public
0
Workaround(s)
view

Description

Consider the following function

function foo
{
    param(
        [string]$name,
        [parameter(mandatory=$true)]
        [system.security.securestring]$password
    )

}

If I specify the password I get an error (expected)

PS> foo -name shay -password onlyme
foo : Cannot process argument transformation on parameter 'password'. Cannot convert the "onlyme" value of type
"System.String" to type "System.Security.SecureString".
At line:1 char:15
+ foo -password onlyme
+             ~~~~~~
    + CategoryInfo         : InvalidData: (:) [foo], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,foo


But if I leave it out, PowerShell prompts me to supply mandatory arguments, only now it do the transformation for me and it also mask the password. .

PS> foo

cmdlet foo at command pipeline position 1
Supply values for the following parameters:
password: ******
PS>


We can work around the error by explicitly converting the password to a secure string but I'd like to enjoy both worlds and suggest that PowerShell detect the parameter type and behave just as if the parameter was missing, allowing me to type masked password on the command line and also converting it.

PS> foo -name shay -password *******
Details
Sign in to post a comment.
Sign in to post a workaround.