Difference between a required and optional positional parm

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5403

    by Sdedot at 2013-01-05 06:22:54

    Hello,

    I've invested in the POSH3 book and so far, so good. I'm now reading Chap. 4 and I would like to clarify my understanding on a point regarding positional parameter syntax. Is a required positional parameter denoted by the parameter surrounded by [] and nothing surrounding the value (e.g. [-Logname] ) as oppossed to a optional positional parameter denoted by the parameter and its value surrounded by [] (e.g. [[-Path] ] [[-Filter] ] )? In the later case, other parameters which are not positional use this same syntax, so is the only way to determine if the parameter is positional or not is the help file telling me its a positional parameter? Im referencing your example on page 41 in section 4.5.3.

    Thanks in advance.

    by nohandle at 2013-01-05 11:43:45

    I don't know what book you are reffering to but you can find out the info in the help. Specify the Full parameter to get the full help.
    get-help get-childItem -Full
    -Path

    Required? false
    Position? 0
    Accept pipeline input? true (ByValue, ByPropertyName)
    Parameter set name Items
    Aliases None
    Dynamic? false

    You set the position by option in the param definition. Refer to advanced powershell functions.

    by MattG at 2013-01-06 07:07:13

    Your understanding is correct. A required parameter will not have brackets around the entire parameter (name and type). A required positional parameter will not have brackets around the entire parameter as I just described but it will have brackets around the parameter name as you indicated in your example. Of course, when in doubt, just add '-Full' to Get-Help and it will spell out to you which params are optional, required, positional, etc.

    As a last thought, just try experimenting with various parameter configurations to see first hand how they are reflected in Get-Help. For example, observe the differences in the following commands:
    function Test-Function
    {
    [CmdletBinding()] Param (
    [Parameter(Mandatory = $True)]
    [Int32]
    $Param1,

    [Parameter(Position = 1)]
    [String]
    $Param2
    )
    }
    NAME
    Test-Function

    SYNTAX
    Test-Function [[-Param2] ] -Param1 []
    function Test-Function
    {
    [CmdletBinding()] Param (
    [Parameter(Position = 0, Mandatory = $True)]
    [Int32]
    $Param1,

    [Parameter(Position = 1)]
    [String]
    $Param2
    )
    }
    NAME
    Test-Function

    SYNTAX
    Test-Function [-Param1] [[-Param2] ] []

You must be logged in to reply to this topic.