Consider the following function
Function IfFunctionExistsExecute
{
param ([parameter(Mandatory=$true)][string]$func)
begin
{
# ...
}
process
{
if(Get-Command $func -ea SilentlyContinue)
{
& $func # the amperersand invokes the function instead of just printing the variable
}
else
{
# ignore
}
}
end
{
# ...
}
}
Usage:
Function Foo { "In Foo" }
IfFunctionExistsExecute Foo
This works.
However this doesn't work:
Function Foo($someParam)
{
"In Foo"
$someParam
}
IfFunctionExistsExecute Foo "beer"
However this gives me the ugly error:
IfFunctionExistsExecute : A positional parameter cannot be found that accepts argument 'beer'.
At C:\PSTests\Test.ps1:11 char:24
+ IfFunctionExistsExecute <<<< Foo "beer"
+ CategoryInfo : InvalidArgument: (:) [IfFunctionExistsExecute], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,IfFunctionExistsExecute
How can I do this in PS?