BACKGROUND

  • I am using Powershell 2.0 on Windows 7.
  • I am writing a cmdlet in a Powershell module ("module" is new to Powershell 2.0).
  • To test the cmdlet I am writing Unit tests in Visual Studio 2008 that programmatically invoke the cmdlet.

REFERENCE

  • This Article on MSDN called "How to Invoke a Cmdlet from Within a Cmdlet" shows how to call a cmdlet from C#.

THE SOURCE CODE

  • This is a distilled version of my actual code — I've made it as small as possible so that you can see the problem I am having clearly:

    using System;
    using System.Management.Automation;   
    
    namespace DemoCmdLet1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var cmd = new GetColorsCommand();
    
                    foreach ( var i in cmd.Invoke<string>())
                    {
                       Console.WriteLine("- " + i );   
                    } 
               } 
           } 
    
        [Cmdlet("Get", "Colors")]
        public class GetColorsCommand : Cmdlet
        {
            protected override void ProcessRecord()
            {
                this.WriteObject("Hello");
                this.WriteVerbose("World");
            }
    
        }
    }
    

COMMENTS

  • I understand how to enable and capture verbose output from the Powershell command line; that's not the problem.
  • In this case I am programmatically invoking the cmdlet from C#.
  • Nothing I've found addresses my specific scenario. Some articles suggest I should implement my own PSHost, but seems expensive and also it seems like a have to call the cmdlet as text, which I would like to avoid because that is not as strongly typed.

UPDATE ON 2009-07-20

Here is is the source code based on the answer below

Edit
Report