c# - PSEXEC 通过命令提示符运行成功,但在 ASP .Net/C# 中失败

标签 c# asp.net psexec

我尝试使用 PSEXEC 工具在远程计算机上运行命令,它使用命令提示符成功运行,但在 ASP.NET 项目中失败,显示以下输出错误。

PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich Sysinternals - www.sysinternals.com

The handle is invalid.

Connecting to lnxdevx...

Starting PSEXESVC service on lnxdevx...

Connecting with PsExec service on lnxdevx...

Error deriving session key:.

这是我的示例 C# 代码。

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = "-u xxxxxx -p xxxxxxxxxx \\\\lnxdevx -i -d -w \"C:\\DIRECTORY\" cmd.exe /C dir";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string errormessage = p.StandardError.ReadToEnd();
p.WaitForExit();

最佳答案

问题是您不必打开 psexec,而是打开 cmd,然后从那里运行 psexec。

您尝试执行的代码也是错误的,您需要使用

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe"; \\if it is in System32
startInfo.Arguments = "psexec -u xxxxxx -p xxxxxxxxxx \\machine *your stuff*";
process.StartInfo = startInfo;
process.Start();

我没有测试,但我很确定这会起作用:)

关于c# - PSEXEC 通过命令提示符运行成功,但在 ASP .Net/C# 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36014096/

相关文章:

asp.net - mvc 在没有 httpContext 或 Controller 上下文的情况下渲染部分 View

powershell - 使用Psexec运行Powershell脚本时出错

c# - 使用 SSIS 数据流任务从平面文件源以编程方式派生列

c# - TFS Rest API 查询

c# - 如何在 ListView 中显示类中的嵌套列表

javascript - RegisterClientScript 不适用于设置数组值

c++ - 程序的本地执行不需要回车。远程执行呢?

perl - 在perl中使用系统cmd时超时

c# - StackExchange.Redis可以用来存储POCO吗?

c# - 使用 json 和 ASP MVC 生成对象数组