views:

22

answers:

1

I'm trying to create a cmdlet that calls Powershell function. Can this be done?

Idea is to have static cmdlet that enumerates a set of data and then calls defined function to do something for each item. I can always copy - paste a base template for the enumaration part, but it really easy to make errors while making modification to parameters etc.

+1  A: 

Sure, use the InvokeCommand property on PSCmdlet (assuming you derive from this base class) e.g.:

Collection<PSObject> result = this.InvokeCommand.InvokeScript("somefunc", true,
                                 PipelineResultTypes.None, null, new[] {1,2,3});

Note that in this case somefunc takes three parameters (1,2,3) and no pipeline input (pass null for the fourth parameter).

Keith Hill
It took me for a while, but then I found it. I was wondering why I didn't get any values. Function name should be somefunc $args[0] $args[1] $args[2], if there are three parameters.