0

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);
}

7
  • Maybe you'd find out why if you removed -ErrorAction SilentlyContinue (don't forget to inspect pipeline.Error after execution) :) Commented Aug 12, 2020 at 11:35
  • I played with the command but the output is the same. -ErrorAction SilentlyContinue -This syntax only catch errors and does nothing with the code:( Commented Aug 12, 2020 at 11:39
  • Psychic debugging: your ISE is running as Administrator, your program is not. Commented Aug 12, 2020 at 11:51
  • Your ISE says it's running as administrator. Your C# process probably isn't. It will see different things Commented Aug 12, 2020 at 11:51
  • My application is also running as administrator :) Commented Aug 12, 2020 at 11:58

1 Answer 1

1

For some reason, Get-Process doesn't return the Path of some processes (probably the 64-bit ones) when running in 32-bit mode, so passing those to Get-Item fails to find anything.

You can see this by running your PS script from the x86 PS ISE.

enter image description here

So if you switch your C# Form to 64-bit, it will return all processes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.