The image shows the same powershell command executed in a C# and Powershell ISE. My question is, Powershell shows complete list of process and process directory, but the result of the code executed in C# does not contain windows processes like svchost.exe.
Of course, both my C# Form and Powershell ISE are running as Administrator. Also, I made sure to code my form to launch with administrative privileges. To be double sure, I executed my C# by right click - Run as Administrator (what I also did with Powershell ISE)
Command/s: Powershell and C#
get-process | get-item -erroraction silentlycontinue | format-table name, directory
get-process | format-table name, directory
Although this particular command shows all the process:
get-process
Unfortunately, combining it with the syntax above will show different output from C#.
private string RunScript(string script)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject pSObject in results)
stringBuilder.AppendLine(pSObject.ToString());
return stringBuilder.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
output.Clear();
output.Text = RunFilePs1(input.Text);
}


-ErrorAction SilentlyContinue(don't forget to inspectpipeline.Errorafter execution) :)