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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.