Functions are just scripblocks that are stored in the function provider.
Let's take a look at this. We'll define a function
PS (1) > function hi { "Hi" }
PS (2) > hi
Hi
Now let's use the call operator on the function:
PS (3) > & $function:hi
Hi
Why does this work? Let's take a look at the type of the bject returned from
the function provider:
PS (4) > $function:hi.gettype().Fullname
System.Management.Automation.ScriptBlock
So - anywhere you can take a scriptblock, you can pass a function by doing
$function:name
PS (5) > function foo {$_ -gt 3}
PS (6) > 1..5 | where $function:foo
4
5
Now dreeschkind's post also brings up the use of CommandInfo objects to do
indirect invocation of commands. This is also an interesting technique. The
& (call) operator can take the name of a command, a scriptblock or a command
info object. Internally that's how this works anyway - when you pass a
string to &, it just calls get-command to get the command info object of the
command to execute. This works with functions:
PS (7) > $ci = get-command hi
PS (8) > $ci.gettype().Fullname
System.Management.Automation.FunctionInfo
PS (9) > & $ci
Hi
And cmdlets:
PS (10) > $ci = get-command get-date
PS (11) > $ci.GetType().Fullname
System.Management.Automation.CmdletInfo
PS (12) > & $ci
Sunday, October 22, 2006 11:17:39 AM
It even works with script or external commands:
PS (13) > $ci = get-command cmd.exe
PS (14) > $ci.GetType().FullName
System.Management.Automation.ApplicationInfo
PS (15) > &$ci /c echo hi
hi
The interesting thing here is that the command info object is an absolute
reference to a particular command. So - if you want to make sure you're
always calling a particular command and bypass command lookup, this let's
you do it.
BTW - all of ths material (and more) is covered in chapter 8 of my book.
-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
My Book:
http://manning.com/powershell
"dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
news

1708A5B-FBCB-4942-9FEC-10BFDCDA4CF8@microsoft.com...
> Well, as I tried to point out on IRC, I think these are basically the only
> ways to do that:
>
> function myfunct([string] $astring ) { "this is a $astring " }
>
> # parameter of type ScriptBlock
> function taker ([scriptblock] $test) { process{ &$test $_} }
> 1..10 | taker $(invoke-expression ('{' + (gi function:myfunct).definition
> +
> '}'))
>
> # parameter of type FunctionInfo
> function taker ([System.Management.Automation.FunctionInfo]$test) {
> process{
> &$test $_} }
> 1..10 | taker (gi function:myfunct)
>
> --
> greetings
> dreeschkind
>
> "klumsy@xtra.co.nz" wrote:
>
>> how can you pass a function to a function..
>>
>> i.e here i define a function, it takes whatever is in the pipeline, and
>> calls a function that is passed through as a script block to it
>>
>> function taker ([scriptblock] $test) { process{ &$test $_} }
>>
>> and i can call it like this
>>
>> 1.. 10 | taker {param([int]$inn) $inn * 3}
>>
>> this things i don't like that this is the [scriptblock] is not typed,
>> unlike say a C# delegate. and also i haven't mangaged the syntax to
>> actually pass in a real function
>>
>> like
>>
>> function myfunct([string] $astring ) { "this is a $astring " }
>>
>> 1..10 | taker myfunct
>> 1..10 | taker {myfunct}
>>
>> etc all fail.. i'm sure with this last part its just a syntax error
>> somewhere
>>
>> however the other problem is defining a parameter that doesn't just
>> take any old scriptblock but a scriptblock that is a function of a
>> particular signature
>>
>>