Disable partial parameters

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Bert Levrau Bert Levrau 1 year, 9 months ago.

  • Author
    Posts
  • #30273
    Profile photo of Bert Levrau
    Bert Levrau
    Participant

    I have a code looking somthing like this:

    
    Param
    (
    [Parameter(Mandatory=$true)]
    [String]$ToExecute,
    [String]$Parent,
    [String]$ParentInfo,
    [Parameter(ValueFromRemainingArguments=$true)]
    [System.Collections.Arraylist]$Arguments
    )
    

    I want to be able to use "& script.ps1 -ToExecute 'c:\test.ps1' -P 'test'
    and it has to put the "-P 'test'" as to items in the array
    It works with "& script.ps1 -ToExecute 'c:\test.ps1' -A 'test' but not with -P because i have optional parameters that start with a P.

    Can I disable that partial parameters stuff?

  • #30274
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You can leverage Alias to set the parameter to just the letter:

    function Test-It {
        param (
            [Alias('F')]
            [int]$Foo,
            [Alias('FS')]
            [int]$FileSize
        )
        begin { }
        process { "Processing $Foo" }
        end { }
    }
    
    Test-It -F 1
    
  • #30281
    Profile photo of David Johnson
    David Johnson
    Participant

    If I understand you right, you want the parameter $Arguments to contain two entries, "-P" and "test".
    The ValueFromRemainingArguments option does exactly that, remaining arguments. Powershell will try and bind the other named parameters first.
    To get round that, you'll have to give it something to bind to.
    In the following script:

    Function Test-Args {
    [CmdletBinding()]
    Param
    (
    [Parameter(Mandatory=$true)]
    [String]$ToExecute,
    [String]$Parent,
    [String]$ParentInfo,
    [Parameter(ValueFromRemainingArguments=$true)]
    [System.Collections.Arraylist]$Arguments
    )
    
        "Arguments:"
        For ($i=0;$i -lt @($Arguments).Length;$i++)
        {
            "Arg $i : $($Arguments[$i])"
        }
    
    }
    

    Running this: (note the quotes round '-P')

    Test-Args -Parent $Null -ParentInfo $Null -ToExecute 'do this' '-P' '5'

    produces the following output:
    Arguments:
    Arg 0 : -P
    Arg 1 : 5

  • #30294
    Profile photo of Bert Levrau
    Bert Levrau
    Participant

    David,

    you are near the solution but I have more optional parameters then my example and it is not workable to always pass them with a $null value.

    is there a posibility to disable the use of partial parameters or parameter abriviation?

  • #30299
    Profile photo of David Johnson
    David Johnson
    Participant

    Not to my knowledge. I can't think of another way of doing it other than declaring $Arguments directly in your command line or through splatting.
    You could maybe build the splat programmatically via something along the following lines:

    $MySplat = New-Object system.collections.hashtable
    $MySplat = Get-Help Test-Args | Select -expand Parameters | Select -expand Parameter | Select Name | ForEach { $MySplat.Add($_.Name,$Null) }
    $MySplat['Parameter1IWant'] = 'foo'
    $MySplat['Parameter2IWant'] = 'bar'
    $MySplat['ArrayParameter3IWant'] = 'foo','bar'
    
    Test-Args @MySplat
    

    Would need a rethink if you've got different Parameter Set Names.

  • #30301
    Profile photo of Bert Levrau
    Bert Levrau
    Participant

    David,

    Thanks for the good support.
    For the moment I use a work around. I renamed all my parameters to start with a '_'
    So they will nevver conflict with the dynamical parameters I pass to $Arguments.

    So I changed it to:

    
    Param
    (
    [Parameter(Mandatory=$true)]
    [String]${_ToExecute},
    [String]${_Parent},
    [String]${_ParentInfo},
    [Parameter(ValueFromRemainingArguments=$true)]
    [System.Collections.Arraylist]${_Arguments}
    )
    

    Now there is a visable distinction between the parameters for the script
    (they start with -_)
    and the parameters that will be stored in ${_Arguments}

You must be logged in to reply to this topic.