c# - Cmdlet verbose stream -


i'm writing c# cmdlet using powershell 5.0 sdk.

i'm trying pipe standarderror of third party executable cmdlet output when run powershell in "real time".

i'm using medallionshell library handle running process. i've tried normal c# win form , used command.standarderror.pipetoasync(console.openstandardoutput()) output print executable generated console in "real time".

i tried create own stream object calls writeverbose didn't seem print powershell screen (i passing -verbose cmdlet when run it).

my current flow looks this:

  1. open powershell ise
  2. load module (c# dll)
  3. call cmdlet parameters
    • command.run
    • command.standarderror.pipetoasync(???)
    • command.wait (during step, output should flowing powershell window)
    • check command.result.success.

can point me in right direction on this?

you can not call cmdlet's write methods (like writeverbose) arbitrary thread. need marshal calls methods pipeline thread. way implement message loop, process messages others threads, when other threads want invoke in pipeline thread.

add-type @‘     using system;     using system.collections.concurrent;     using system.diagnostics;     using system.management.automation;     using system.threading;     [cmdlet(verbslifecycle.invoke, "process")]     public class invokeprocesscmdlet : cmdlet {         [parameter(position = 1)]         public string filename { get; set; }         [parameter(position = 2)]         public string arguments { get; set; }         protected override void endprocessing() {             using(blockingcollection<action> messagequeue = new blockingcollection<action>()) {                 using(process process = new process {                     startinfo=new processstartinfo(filename, arguments) {                         useshellexecute=false,                         redirectstandardoutput=true,                         redirectstandarderror=true                     },                     enableraisingevents=true                 }) {                     int numberofcompleterequests = 0;                     action complete = () => {                         if(interlocked.increment(ref numberofcompleterequests)==3) {                             messagequeue.completeadding();                         }                     };                     process.outputdatareceived+=(sender, args) => {                         if(args.data==null) {                             complete();                         } else {                             messagequeue.add(() => writeobject(args.data));                         }                     };                     process.errordatareceived+=(sender, args) => {                         if(args.data==null) {                             complete();                         } else {                             messagequeue.add(() => writeverbose(args.data));                         }                     };                     process.exited+=(sender, args) => complete();                     process.start();                     process.beginoutputreadline();                     process.beginerrorreadline();                     foreach(action action in messagequeue.getconsumingenumerable()) {                         action();                     }                 }             }         }     } ’@ -passthru | select-object -first 1 -expandproperty assembly | import-module 

and can test this:

invoke-process icacls 'c:\* /c' -verbose 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -