简体   繁体   English

当我从 C# 创建进程时 psexec.exe 不起作用

[英]psexec.exe doesn't work when I create a Process from C#

Long story short...长话短说...

This doesnt work:这不起作用:

Process p = new Process();
p.StartInfo.FileName = @"External\PsExec.exe";
string file = String.Concat(Path.Combine(Environment.CurrentDirectory,"temp"),@"\iisreset",DateTime.Now.ToString("ddMMyyyy-hhmmssss"),".txt");
p.StartInfo.Arguments = String.Format("-s -u {0}\\{1} -p {2} \\\\{3} iisreset > \"{4}\"", Domain,UserName, Password, machineIP, file);
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();

I'm getting a RPC Unavailable message.我收到一条 RPC Unavailable 消息。

But when I access the command line in the program folder, then i run this: (with the correct parameters), exactly like I specified in the filename/arguments...但是当我访问程序文件夹中的命令行时,我运行这个:(使用正确的参数),就像我在文件名/参数中指定的那样......

External\PsExec.exe -s -u [user] -p [password] \\[ip] iisreset > "[path]"

It works!有用! Do I have to specify anything else in the C# Process ?我是否必须在 C# 进程中指定其他任何内容? What could be possibly happening?可能会发生什么?

Thanks in advance!提前致谢!

EDIT : It works if I put cmd as the FileName and /c PsExec.exe before the arguments.编辑:如果我将cmd作为 FileName 和/c PsExec.exe放在参数之前,它会起作用。 The problem is this way it always show the window.问题是这样它总是显示窗口。

Instead of using p.startinfo.arguments use p.standardinput.writeline(command)而不是使用 p.startinfo.arguments 使用 p.standardinput.writeline(command)

string PSPath = @"C:\PSTools\PsExec.exe";
            fullcommand = PSPath + " -u " + userName + " -p " + password + " \\\\" + remoteMachine + " -h cmd.exe /c " + command + "";
            

            Console.Clear();
            //Console.WriteLine(fullcommand);
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardInput = true;

            process.StartInfo.FileName = "cmd.exe";
            //process.StartInfo.Arguments = fullcommand;

            process.Start();
            process.StandardInput.WriteLine(fullcommand);
            process.StandardInput.Flush();
            process.StandardInput.Close();
            Console.WriteLine("*****Command*****");
            Console.WriteLine(fullcommand);
            Console.WriteLine("*****Output*****");
            Console.WriteLine(process.StandardOutput.ReadToEnd());
            Console.WriteLine("*****Error*****");
            Console.WriteLine(process.StandardError.ReadToEnd());
            Console.WriteLine("*****Exit*****");
            process.WaitForExit();
            Console.WriteLine("Again ?");

You cannot redirect standard output using the arguments the way you are doing.您不能按照您的方式使用参数重定向标准输出。 That's not how things actually work.实际情况并非如此。

At the command line, your arguments end when the command interpreter sees the >, and it begins the process of redirecting standard output to the filename.在命令行中,当命令解释器看到 > 时,您的参数结束,并开始将标准输出重定向到文件名的过程。

To accomplish this in C# you need to use the RedirectStandardOutput property of the StartInfo class, then read from the Process.StandardOutput stream and write to a file.要在 C# 中完成此操作,您需要使用StartInfo类的RedirectStandardOutput属性,然后从Process.StandardOutput流中读取并写入文件。

The MSDN documentation for RedirectStandardOutput has a short example you can use to get started. RedirectStandardOutput的 MSDN 文档有一个简短的示例,您可以使用它来开始。

iisreset [machinename] -

你不需要 psexec

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM