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

发布于 2024-12-14 09:37:28 字数 854 浏览 0 评论 0原文

长话短说...

这不起作用:

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 Unavailable 消息。

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

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

它有效! 我必须在 C# Process 中指定其他内容吗?可能会发生什么?

提前致谢!

编辑:如果我将 cmd 作为文件名并将 /c PsExec.exe 放在参数之前,它就可以工作。问题是这样它总是显示窗口。

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.

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 ? 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. The problem is this way it always show the window.

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

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

发布评论

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

评论(3

你没皮卡萌 2024-12-21 09:37:28

使用 p.standardinput.writeline(command) 而不是使用 p.startinfo.arguments

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 ?");

Instead of using p.startinfo.arguments use 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 ?");
静谧 2024-12-21 09:37:28

您无法按照您的方式使用参数重定向标准输出。事情的实际情况并非如此。

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

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

RedirectStandardOutput 的 MSDN 文档 有一个简短的示例,您可以使用它来入门。

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.

The MSDN documentation for RedirectStandardOutput has a short example you can use to get started.

木森分化 2024-12-21 09:37:28
iisreset [machinename] -

你不需要 psexec

iisreset [machinename] -

you don't need psexec

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