9

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.

Some things are still not clear to me: * How to call the "Get-Colors" cmdlet (ideally without having to pass it as a string to the ps objet) * How to get the verbose output as it is generated instead of getting an collection of them at the end.

    using System;
    using System.Management.Automation;   

    namespace DemoCmdLet1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var ps = System.Management.Automation.PowerShell.Create();

                ps.Commands.AddScript("$verbosepreference='continue'; write-verbose 42");

                foreach ( var i in ps.Invoke<string>())
                {
                   Console.WriteLine("normal output: {0}" , i );   
                }
                foreach (var i in ps.Streams.Verbose)
                {
                    Console.WriteLine("verbose output: {0}" , i);
                }

            }
        }

        [Cmdlet("Get", "Colors")]
        public class GetColorsCommand : Cmdlet
        {
            protected override void ProcessRecord()
            {
                this.WriteObject("Red");
                this.WriteVerbose("r");
                this.WriteObject("Green");
                this.WriteVerbose("g");
                this.WriteObject("Blue");
                this.WriteVerbose("b");

            }

        }
    }

The code above generates this output:

d:\DemoCmdLet1\DemoCmdLet1>bin\Debug\DemoCmdLet1.exe
verbose output: 42

UPDATE ON 2010-01-16

by using the Powershell class (found in System.Management.Automation but only in the version of the assembly that comes with the powershell 2.0 SDK, not what comes out-of-the-box on Windows 7) I can programmatically call the cmdlet and get the verbose output. The remaining part is to actually add a custom cmdlet to that powershell instance - because that was my original goal - to unit test my cmdlets not those that come with powershell.

class Program
{
    static void Main(string[] args)
    {
        var ps = System.Management.Automation.PowerShell.Create();
        ps.AddCommand("Get-Process");
        ps.AddParameter("Verbose");
        ps.Streams.Verbose.DataAdded += Verbose_DataAdded;
        foreach (PSObject result in ps.Invoke())
        {
            Console.WriteLine(
                    "output: {0,-24}{1}",
                    result.Members["ProcessName"].Value,
                    result.Members["Id"].Value);
        } 
        Console.ReadKey();
    }

    static void Verbose_DataAdded(object sender, DataAddedEventArgs e)
    {
        Console.WriteLine( "verbose output: {0}", e.Index);
    }
}


[Cmdlet("Get", "Colors")]
public class GetColorsCommand : Cmdlet
{
    protected override void ProcessRecord()
    {
        this.WriteObject("Hello");
        this.WriteVerbose("World");
    }
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
namenlos
  • 5,111
  • 10
  • 38
  • 38
  • 1
    there was a missing backtick ` in my answer - this prevents the $verbosepreference variable being evaluated ahead of time. – x0n Jul 19 '09 at 20:33
  • Why haven't you marked my response as the answer? – x0n Jan 12 '10 at 04:12
  • If you want real-time verbose records, subscribe to the datachanged event on the streams property of the powershell instance. – x0n Jan 12 '10 at 04:13
  • I'vee upated the question to account for the new info you have provided. I still can't say this question is answered yet as I have not figured out how to add the "Get-Colors" cmdlet to the Powershell instance. – namenlos Jan 17 '10 at 07:02
  • 1
    You probably should have asked a new question. As I answered your question, then you added a new question to the old question. – x0n Jan 10 '11 at 21:18

3 Answers3

10
  • Verbose output is not actually output unless $VerbosePreference is set at least to "Continue."
  • Use the PowerShell type to run your cmdlet, and read VerboseRecord instances from the Streams.Verbose propery

Example in powershell script:

ps> $ps = [powershell]::create()
ps> $ps.Commands.AddScript("`$verbosepreference='continue'; write-verbose 42")
ps> $ps.invoke()
ps> $ps.streams.verbose
Message   InvocationInfo                          PipelineIterationInfo
-------   --------------                          ---------------------
42        System.Management.Automation.Invocat... {0, 0}

This should be easy to translate into C#.

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
x0n
  • 51,312
  • 7
  • 89
  • 111
0
1.        string scriptFile = "Test.ps1";
2.        using (PowerShell ps = PowerShell.Create())
3.        {
4.          const string getverbose = "$verbosepreference='continue'"; 
5.          ps.AddScript(string.Format(getverbose));
6.          ps.Invoke();
7.          ps.Commands.Clear();
8.          ps.AddScript(@".\" + scriptFile);
9.          ps.Invoke();
10.         foreach (var v in ps.Streams.Verbose)
11.         {
12.               Console.WriteLine(v.Message);
13.         }
14.       }

Important lines are line 5 and 6. This basically set the $verbosepreference for the session and for upcoming new commands and scripts.

Varun
  • 743
  • 1
  • 5
  • 11
0

First off, if you are unit testing cmdlets, likely Pester is a better (and easier) option.

As per your many updates, all you are likely missing at this point is a strongly typed approach to reference the C# cmdlet

ps.AddCommand(new CmdletInfo("Get-MyCS", typeof(GetMyCS)));

DISCLAIMER: I know this works for PowerShell 5.0, but don't have experience with the older PowerShell 2.0.

carrvo
  • 511
  • 5
  • 11