Consider this:
Function Foo
{
param(
#????
)
}
I want to call Foo like this:
Foo -Bar "test"
Without it blowing up that I don't have a $bar param specified. Is that possible? :)
Update:
I want this to work:
Function IfFunctionExistsExecute
{
param ([parameter(Mandatory=$true)][string]$func, [parameter(Mandatory=$false)][string]$args)
begin
{
# ...
}
process
{
if(Get-Command $func -ea SilentlyContinue)
{
& $func $args # the amperersand invokes the function instead of just printing the variable
}
else
{
# ignore
}
}
end
{
# ...
}
}
Function Foo
{
param([string]$anotherParam)
process
{
$anotherParam
}
}
IfFunctionExistsExecute Foo -Test "bar"
This gives me:
IfFunctionExistsExecute : A parameter cannot be found that matches parameter name 'Test'.
At C:\PSTests\Test.ps1:35 char:34
+ IfFunctionExistsExecute Foo -Test <<<< "bar"
+ CategoryInfo : InvalidArgument: (:) [IfFunctionExistsExecute], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,IfFunctionExistsExecute
I would suggest two alternatives.
First: you may want to consider passing whole function + it's parameters as scriptblock parameter to your ifFunction...
OR: use ValueFromRemainingArguments:
function Test-SelfBound {
param (
[Parameter(
Mandatory = $true,
HelpMessage = 'Help!'
)]
[string]$First,
[Parameter(
ValueFromRemainingArguments = $true
)]
[Object[]]$MyArgs
)
$Arguments = foreach ($Argument in $MyArgs) {
if ($Argument -match '^-([a-z]+)$') {
$Name = $Matches[1]
$foreach.MoveNext() | Out-Null
$Value = $foreach.Current
New-Variable -Name $Name -Value $Value
$PSBoundParameters.Add($Name,$Value) | Out-Null
} else {
$Argument
}
}
$PSBoundParameters | Out-Default
"Positional"
$Arguments
}
Test-SelfBound -First Test -Next Foo -Last Bar Alfa Beta Gamma
In this case I use $MyArgs to store everything besides my mandatory parameter 'First'. Than some simple if will tell me if it's named parameter (-Next, -Last) or positional (Alfa, Beta, Gamma). This way you can have both advantages of advanced functions binding (whole [Parameter()] decoration) AND leave room for $args-style parameters too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With