powershell - In C# running the PoweShell script first and then invoke Get-Process returns 0 result -
in c# code have:
powershell ps = powershell.create(); ps.addscript(". ...\\windowspowershell\\microsoft.powershell_profile.ps1"); foreach (psobject result in ps.invoke()) { console.writeline(result.tostring()); } ps.addcommand("get-process"); icollection<psobject> results = ps.invoke(); console.writeline(results.count); foreach (psobject result in results) { console.writeline("---" + result.tostring()); }
if run get-process without running script before hand (by commenting out ps.addscript part , loop), get-process returns results; code provided return 0 get-process.
any idea why 0?
after each invoke, if want start new command instead of passing command pipeline previous command, should clear first: ps.commands.clear();
the complete code is:
powershell ps = powershell.create(); ps.addscript(". ...\\windowspowershell\\microsoft.powershell_profile.ps1"); foreach (psobject result in ps.invoke()) { console.writeline(result.tostring()); } ps.commands.clear(); ps.addcommand("get-process"); icollection<psobject> results = ps.invoke(); console.writeline(results.count); foreach (psobject result in results) { console.writeline("---" + result.tostring()); }
now results.count displays correct row count , result.tostring() prints out correct value.
Comments
Post a Comment