在 C# 中获取 PsExec.exe 的进程 ID 需要帮助吗?

发布于 2024-08-13 23:20:08 字数 814 浏览 6 评论 0原文

我使用下面的代码来调用 PsExec.exe,它在两个服务器中调用我的控制台应用程序,我无法获取被调用进程(我的控制台应用程序)的 ProcessId。

process.StandardOutput.ReadToEnd());只给我服务器名称而不是完整的内容。

您能帮我获取远程服务器上 PsExec.exe 生成的进程 ID 吗?

        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:\data\GridWorker\GridWorker.exe 100000";
        process.StartInfo = psi;
        process.Start();

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

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:\data\GridWorker\GridWorker.exe 100000";
        process.StartInfo = psi;
        process.Start();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由如风 2024-08-20 23:20:08

尝试将 -d 参数添加到 PsExec 命令行。

不要等待申请
终止。仅将此选项用于
非交互式应用程序。

这应该正确地将进程 ID 返回到 StandardError。

示例:

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

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

输出:

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

C:\WINDOWS\system32\mspaint.exe started with process ID 5896.

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:\WINDOWS\system32\mspaint.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:\WINDOWS\system32\mspaint.exe started with process ID 5896.
心欲静而疯不止 2024-08-20 23:20:08

我不认为你可以让 PsExec 以你想要的方式返回 pid。

但是,您可以做的是将自己的应用程序启动器包装器编写为控制台应用程序,并让它返回 pid。然后,您可以让 PsExec 始终通过调用此“AppStarter”来启动应用程序,从而返回您的 pid。

大致如下:

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

[这是一个粗略且准备好的示例,没有异常处理等 - 只是作为说明]

上面的代码现在变得像一样

 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:\data\GridWorker\GridWorker.exe 100000";
    process.StartInfo = psi;
    process.Start();

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

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:\data\GridWorker\GridWorker.exe 100000";
    process.StartInfo = psi;
    process.Start();

    Console.WriteLine(process.StandardOutput.ReadToEnd());
ら栖息 2024-08-20 23:20:08

到目前为止,我理解了最初的问题,任务是获取远程计算机上远程启动的进程的 PID。这是真的吗?在这种情况下,所有答案都没有真正的帮助。

您必须为每台远程计算机创建 WMI 查询,以获取已启动的进程。这可以使用“Win32_ProcessStartTrace”类来完成。

如果您需要更多帮助,请告诉我。

br--马布拉

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文