2

I trying to run my custom PowerShell cmdlets from another custom cmdlets. The strange thing is that I can run my custom cmdlets manually from the Windows Powershell and it works, but when I try to trigger the same custom cmdlets it fails.

Powershell

Run-Demo code:

It's basically like this the Run-Demo: I know it missing the parameter, but 'never the less' it should find the cmdlets in PowerShell.

                Command cmd = new Command("Run-ContentInventory");
                Runspace runSpace = RunspaceFactory.CreateRunspace();
                runSpace.Open();
                Pipeline pipeline = runSpace.CreatePipeline();
                pipeline.Commands.Add(cmd);
                Collection<PSObject> output;
                output = pipeline.Invoke();
                foreach (PSObject psObject in output)
                {
                    Console.WriteLine(psObject);
                }

Thanks for any help!

3
  • Could you post the code for "Run-Demo"? Commented Jan 22, 2014 at 19:09
  • don't you have to import it first ? Commented Jan 22, 2014 at 19:24
  • If you deploy the cmdlets from Visual-Studio it will import. But I have also tried to import(-module) the dll created manually when building the solution. Same outcome. Commented Jan 22, 2014 at 19:28

1 Answer 1

2

The core of your problem is that you're creating a new runspace, which won't have your cmdlets loaded. This is something you should avoid, because it eats up a lot of memory, and creates a lot of problems in trying to synchronize the two runspaces.

The trick is to use a Pipeline. Declare a private field of type Pipeline, then, in your BeginProcessing, add:

 pipeline = Runspace.DefaultRunspace.CreateNestedPipeline();

Please dispose of the nested pipeline in EndProcessing.

In processrecord, your code should remain almost the same.

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

Comments

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.