Search

Mandatory parameter prompts should accept variable input by Trevor Sullivan

Active

7
0
Sign in
to vote
Type: Suggestion
ID: 773554
Opened: 12/5/2012 8:12:40 AM
Access Restriction: Public
0
Workaround(s)
Note: See attachment to this ticket for a video demonstration of the issue.

When writing a PowerShell function, script, or ScriptBlock that accepts named parameters, it is sometimes desirable to have the end user prompted for input. PowerShell input prompts work great for strings and integers, but fails with other data types (eg. bool, object).
Details (expand)
How often does this happen?
Always Happens
Have you seen this problem before in this product?
Yes, this happens in all previous versions
Reproduction Steps
function Test-Parameter {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, HelpMessage = "Please input a Process object")]
        [System.Diagnostics.Process] $input
    )

    Write-Host -Object $input.Description;
}

$Process = (Get-Process)[10];                 # Get a System.Diagnostics.Process object
Write-Host -Object $proc.GetType().FullName; # Make sure we actually have a System.Diagnostics.Process object

Test-Parameter;                                # Call the Test-Parameter function. When prompted for input
                                             # plug in $Process.


#######################################


function Test-Parameter {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, HelpMessage = "Please input a Process object")]
        [bool] $TrueOrFalse
    )

    Write-Host -Object $TrueOrFalse;
}

Test-Parameter; # When prompted, type $false. The function returns $true, because the inputs is evaluated as a string.
Expected Results
You should be able to specify variable input when prompted to input parameter values. For boolean values, you should be able to input $true or $false when prompted. For arbitrary objects, you should be able to specify a variable that contains the object you want to use as input.
File Attachments
File Name Submitted By Submitted On File Size  
ScreenCapture_12-5-2012 10.20.28 AM.wmv 12/5/2012 2.08 MB
Sign in to post a comment.
Posted by Trevor Sullivan on 12/5/2012 at 8:23 AM
Update: Video attachment included on post, which demonstrates the code in the post.

Cheers,
Trevor Sullivan
Sign in to post a workaround.