none
How to use CLR profiler for PowerShell Cmdlets? RRS feed

  • Question

  • We have written a bunch of PowerShell cmdlets and would like to use CLR profiler for dynamic code analysis. If I start PowerShell.exe using CLR profiler, the PowerShell window does not appear, so I cannot type any commands. I could not find a way to supply command line arguments (which could be a PS1 script calling my cmdlets).

    Any idea on how to use CLR profiler for profiling PowerShell cmdlet code?

    Tuesday, August 10, 2010 10:01 PM

Answers

  • Hi,

     

    I cannot reproduce your issue, my test result shows that CLR Profiler can launch PowerShell.exe successfully, and I can input commands.

     

    Since we can execute Power Shell cmdlets in C# code, you may create a simple C# console application in which you invoke your cmdlet, something like:

     

        class Program

        {

            static void Main(string[] args)

            {

                Runspace runSpace = RunspaceFactory.CreateRunspace();

                runSpace.Open();

     

                //

                Pipeline pipeLine = runSpace.CreatePipeline();

     

                Command addSnapin = new Command("Add-PSSnapin");

                addSnapin.Parameters.Add("name", "GetProcPSSnapIn01");

     

                pipeLine.Commands.Clear();

                pipeLine.Commands.Add(addSnapin);

     

                pipeLine.Invoke();

     

                //

                Pipeline pipeLine2 = runSpace.CreatePipeline();

                Command getProcessCStarted = new Command("get-proc");

     

                pipeLine2.Commands.Add(getProcessCStarted);

     

                Collection<PSObject> cNameProcesses = pipeLine2.Invoke();

     

                foreach (PSObject psObject in cNameProcesses)

                {

                    Process process = psObject.BaseObject as Process;

                    Console.WriteLine("Process Name: {0}", process.ProcessName);

                }

            }

    }

     

    Then, you may let CLR Profiler to launch the console application instead of PowerShell.exe.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Anagha_12 Wednesday, August 11, 2010 9:28 PM
    Wednesday, August 11, 2010 9:27 AM

All replies

  • Hi,

     

    I cannot reproduce your issue, my test result shows that CLR Profiler can launch PowerShell.exe successfully, and I can input commands.

     

    Since we can execute Power Shell cmdlets in C# code, you may create a simple C# console application in which you invoke your cmdlet, something like:

     

        class Program

        {

            static void Main(string[] args)

            {

                Runspace runSpace = RunspaceFactory.CreateRunspace();

                runSpace.Open();

     

                //

                Pipeline pipeLine = runSpace.CreatePipeline();

     

                Command addSnapin = new Command("Add-PSSnapin");

                addSnapin.Parameters.Add("name", "GetProcPSSnapIn01");

     

                pipeLine.Commands.Clear();

                pipeLine.Commands.Add(addSnapin);

     

                pipeLine.Invoke();

     

                //

                Pipeline pipeLine2 = runSpace.CreatePipeline();

                Command getProcessCStarted = new Command("get-proc");

     

                pipeLine2.Commands.Add(getProcessCStarted);

     

                Collection<PSObject> cNameProcesses = pipeLine2.Invoke();

     

                foreach (PSObject psObject in cNameProcesses)

                {

                    Process process = psObject.BaseObject as Process;

                    Console.WriteLine("Process Name: {0}", process.ProcessName);

                }

            }

    }

     

    Then, you may let CLR Profiler to launch the console application instead of PowerShell.exe.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Anagha_12 Wednesday, August 11, 2010 9:28 PM
    Wednesday, August 11, 2010 9:27 AM
  • Thanks, writing a C# program works...

    I still cannot launch the PowerShell window, may be because I am running on 64-bit OS (W2K8 R2) with .NET 3.5 SP1?

    Wednesday, August 11, 2010 9:30 PM