1

长话短说...

这不起作用:

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

我收到 RPC 不可用消息。

但是当我访问程序文件夹中的命令行时,我运行这个:(使用正确的参数),就像我在文件名/参数中指定的那样......

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

有用!我是否必须在 C# Process 中指定其他任何内容?可能会发生什么?

提前致谢!

编辑:如果我把文件名放在参数之前,它cmd会起作用/c PsExec.exe。问题是这样它总是显示窗口。

4

3 回答 3

2

而不是使用 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 ?");
于 2020-07-20T13:27:02.703 回答
1

您不能以您正在做的方式使用参数重定向标准输出。这不是事情的实际运作方式。

在命令行中,您的参数在命令解释器看到 > 时结束,并开始将标准输出重定向到文件名的过程。

要在 C# 中完成此操作,您需要使用类的RedirectStandardOutput属性StartInfo,然后从Process.StandardOutput流中读取并写入文件。

RedirectStandardOutput的 MSDN 文档有一个简短的示例,可用于入门。

于 2011-11-09T15:39:57.447 回答
0
iisreset [machinename] -

你不需要 psexec

于 2013-10-11T21:58:16.163 回答