Calling function from Powershell Cmdlet – Education Career Blog

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.

,

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).

Leave a Comment