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