Author Posts

September 30, 2015 at 6:25 am

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?

September 30, 2015 at 7:05 am

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

September 30, 2015 at 8:00 am

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

September 30, 2015 at 9:40 pm

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?

October 1, 2015 at 1:26 am

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.

October 1, 2015 at 2:41 am

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}