I cannot find this specific question, so I will ask it here: It seems that the dynamc parameter is not a parameter of position 0. When I try, it seems that the first static parameter defined in position 1 will advance or inherit position 0, and then the dynamic parameter defined for position 0 is then added to the next available position (position 1):
$x=[string]::Empty;
Function foo {
[cmdletbinding()]
Param (
[Parameter(ParameterSetName="set1",
Position=1,
ValueFromPipeline=$true)]
$InputObject,
[Parameter()]
[switch]
$RequireFilePath
)
DynamicParam {
$mand = $script:x -eq $null -or `
$script:x -eq [string]::Empty -or `
$RequireFilePath.IsPresent;
$attrs = New-Object System.Management.Automation.ParameterAttribute;
$attrs.ParameterSetName = "set1";
$attrs.Mandatory = $mand;
$attrs.Position = 0;
$attrCollection = New-Object `
System.Collections.ObjectModel.Collection[System.Attribute];
$attrCollection.Add($attrs);
$FilePath = New-Object System.Management.Automation.RuntimeDefinedParameter `
"FilePath", string, $attrCollection;
$paramDictionary = New-Object `
System.Management.Automation.RuntimeDefinedParameterDictionary;
$paramDictionary.Add("FilePath", $FilePath);
$paramDictionary;
}
Begin {
if ( $FilePath.Value -eq $null -or $FilePath.Value -eq [string]::Empty) {
$FilePath.Value = $script:x;
} else {
$script:x = $FilePath.Value;
}
Write-Output ("(foo) FilePath: {0}" -f $FilePath.Value);
Write-Output ("(foo) RequireFilePath: {0}" -f $RequireFilePath.IsPresent);
Write-Output ("(foo) script:x: {0}" -f $script:x);
}
Process {
Write-Output ("(foo) InputObject: {0}" -f $InputObject);
}
End {
}
}
foo "filename2.txt" "zxcv";
When executed, I get the following:
(foo) FilePath: zxcv
(foo) RequireFilePath: False
(foo) script:x: zxcv
(foo) InputObject: filename2.txt
I believe my expectation was that the dynamic parameter should have been position 0, and the static parameter should have been position 1. Can anyone weigh this? Is it possible for a dynamic parameter to be defined at a position smaller (earlier) than a static parameter?