我使用下面的代码来调用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());
Run Code Online (Sandbox Code Playgroud)
尝试将-d参数添加到PsExec命令行.
不要等待申请终止.仅对非交互式应用程序使用此选项.
这应该正确地将Process 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());
Run Code Online (Sandbox Code Playgroud)
输出:
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.
Run Code Online (Sandbox Code Playgroud)
我不认为你可以让 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
[这是一个粗略且现成的示例,没有异常处理等 - 只是作为说明]
你上面的代码现在变成了类似的东西
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());
Run Code Online (Sandbox Code Playgroud)