English 中文(简体)
Help needed to get Process ID for PsExec.exe in C#?
原标题:
  • 时间:2009-12-10 08:38:53
  •  标签:
  • c#
  • wmi
  • psexec

I am using the below code to invoke PsExec.exe which invokes my console application in two servers, I am not able to grab the ProcessId of the invoked processes (my console apps).

process.StandardOutput.ReadToEnd()); is only giving me the servernames but not the complete content.

Can you please help me to get the process id s generated by PsExec.exe on the remote servers ??

        Process process = new Process();
        ProcessStartInfo psi = new ProcessStartInfo(@"PsExec.exe");
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.WindowStyle = ProcessWindowStyle.Minimized;
        psi.CreateNoWindow = true;
        psi.Arguments = @"-i -u Username -p xxxxxx \server1,server2 C:dataGridWorkerGridWorker.exe 100000";
        process.StartInfo = psi;
        process.Start();

        Console.WriteLine(process.StandardOutput.ReadToEnd());
问题回答

Try adding the -d parameter to the PsExec commandline.

Don t wait for application to terminate. Only use this option for non-interactive applications.

This should correctly return the Process ID to StandardError.

The example:

ProcessStartInfo psi = new ProcessStartInfo(
    @"PsExec.exe",
    @"-d -i -u user -p password \server C:WINDOWSsystem32mspaint.exe")
                           {
                               UseShellExecute = false,
                               RedirectStandardOutput = true,
                               RedirectStandardError = true,
                               RedirectStandardInput = true,
                               WindowStyle = ProcessWindowStyle.Minimized,
                               CreateNoWindow = true
                           };
Process process = Process.Start(psi);

Console.WriteLine(process.StandardError.ReadToEnd());

Output:

PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com

C:WINDOWSsystem32mspaint.exe started with process ID 5896.

I don t think you can get PsExec to return the pid in the way you want.

However, what you can do is write your own application starter wrapper as a console application, and have this return the pid. You can then have PsExec always start applications by calling this "AppStarter", thus returning your pid.

Something along the lines of:

namespace AppStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo(args[0]);
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.RedirectStandardInput = true;
            psi.Arguments = string.Join(" ", args, 1, args.Length - 1);
            process.StartInfo = psi;
            process.Start();
            Console.WriteLine("Process started with PID {0}", process.Id);
        }
    }
}

[this is a rough and ready example, with no exception handling, etc - just as an illustration]

Your code above now becomes somthing like

 Process process = new Process();
    ProcessStartInfo psi = new ProcessStartInfo(@"AppStarter.exe");
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.RedirectStandardInput = true;
    psi.WindowStyle = ProcessWindowStyle.Minimized;
    psi.CreateNoWindow = true;
    psi.Arguments = @"PsExec.exe -i -u Username -p 26.06.08 \server1,server2 C:dataGridWorkerGridWorker.exe 100000";
    process.StartInfo = psi;
    process.Start();

    Console.WriteLine(process.StandardOutput.ReadToEnd());

So far I understodd the original question, the task was to get the PID of the remoetely started processes on the remote machines. Is this true? In this case, none of the answers are really helpful.

You must create WMI queries for each of you remote computers, to fetch the started processes. This can be done using the "Win32_ProcessStartTrace" classes.

If you need more help, let me know.

br--mabra





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签