let's assume have simple powershell script myscript.ps1, writes standart output:
get-location   now want execute script .net application. ended next code:
var process = new process         {             startinfo = {                 filename = "powershell.exe",                 arguments = "-file \"myscript.ps1\"",                 workingdirectory = path.combine(environment.currentdirectory, "run"),                 useshellexecute = false,                 redirectstandardoutput = true             }         };  process.outputdatareceived += (sender, args) => console.writeline(args.data); process.start(); process.beginoutputreadline(); process.waitforexit();   and works, think should more elegant way using system.management.automation assembly:
using (powershell shell = powershell.create()) {     shell.commands.addscript("myscript.ps1");     var r = shell.invoke();     console.writeline(r); }   but invoke returns collection of `psobject, , can't find way access stdout
on "myscript.ps1" put @ end out-string
Comments
Post a Comment