在窗口窗体中启动 psexec 进程失败



所以我创建了一个表单,允许我们的营销团队重新启动一台为访问者显示幻灯片/重要信息的计算机等

计划是将其部署到其他用户计算机,然后他们可以执行以下三个任务之一: 立即重启 定时/计划重启 取消定时/计划重新启动

该应用程序在我的计算机上运行良好,在调试和构建中运行良好。 在其他人的计算机上安装时,似乎它正在启动psexec,但随后什么也没做。

我添加了一些捕获异常,此时它向我显示以下错误:

标准输出尚未重定向

但是,在研究这个问题时,我似乎已经有了大多数人建议解决此问题的代码。

我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Media_Wall_Restart_Scheduler
{
public partial class frm_mediawall_startform : Form
{
public frm_mediawall_startform()
{
InitializeComponent();
input_time.Value = DateTime.Now;

}
static class Programmability
{
/* Secure Credential Store - This is for remote reboots */
public static readonly string mediawallcomputername = @"\computername";
public static readonly string adminusername = "computeraccount";
public static readonly string admindomainame = "domainname";
public static readonly string adminpassword = "computeraccountpassword";
public static readonly string addadminpassword = " -p " + adminpassword;
public static readonly string adminuseraccount = admindomainame + @"" + adminusername;
public static readonly string addadminuseraccount = " -u " + admindomainame + @"" + adminusername;
public static readonly string Restartnow = mediawallcomputername + addadminuseraccount + addadminpassword + " shutdown -r -t 30";
public static readonly string CancelShutdown = mediawallcomputername + addadminuseraccount + addadminpassword + " shutdown -a";
public static readonly string psexecpath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "Resources", "PsExec.exe"));
/* End Secure credential Store  */
}
private void Btn_restart_now_Click(object sender, EventArgs e)
{
/* Quick and simple Restart now command */
Process Restart_now = new Process();
Restart_now.StartInfo.UseShellExecute = false;
Restart_now.StartInfo.RedirectStandardOutput = true;
Restart_now.StartInfo.RedirectStandardError = true;
Restart_now.StartInfo.RedirectStandardInput = true;
Restart_now.StartInfo.FileName = Programmability.psexecpath;
var Restartnow_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -s -t 30" };
Restart_now.StartInfo.Arguments = string.Join(" ", Restartnow_args);
{
Restart_now.Start();
string Output = Restart_now.StandardOutput.ReadToEnd();
string Errormessage = Restart_now.StandardError.ReadToEnd();
Restart_now.WaitForExit();
}

}
public void Btn_scheduled_restart_Click(object sender, EventArgs e)
{
/* Gets the current time */
DateTime Timenow = DateTime.Now;
DateTime Restarttime = input_time.Value;
/* Takes the Scheduled time and subtracts from now time above */
var ScheduledRestartTime = Math.Ceiling((Restarttime - Timenow).TotalSeconds);
/* Run the command to restart the Media wall at the specifed time */
Process Timed_Restart = new Process();
Timed_Restart.StartInfo.UseShellExecute = false;
Timed_Restart.StartInfo.RedirectStandardOutput = true;
Timed_Restart.StartInfo.RedirectStandardError = true;
Timed_Restart.StartInfo.RedirectStandardInput = true;
Timed_Restart.StartInfo.FileName = Programmability.psexecpath;
var TimedRestart_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -r", $"-t {ScheduledRestartTime}" };
Timed_Restart.StartInfo.Arguments = string.Join(" ", TimedRestart_args);
{
Timed_Restart.Start();
string Output = Timed_Restart.StandardOutput.ReadToEnd();
string Errormessage = Timed_Restart.StandardError.ReadToEnd();
Timed_Restart.WaitForExit();
}

}

private void Btn_stop_restart_Click(object sender, EventArgs e)
{
Process Stop_Restart = new Process();
Stop_Restart.StartInfo.UseShellExecute = false;
Stop_Restart.StartInfo.RedirectStandardOutput = true;
Stop_Restart.StartInfo.RedirectStandardError = true;
Stop_Restart.StartInfo.RedirectStandardInput = true;
Stop_Restart.StartInfo.FileName = Programmability.psexecpath;
var cancel_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -a" };
Stop_Restart.StartInfo.Arguments = String.Join(" ", cancel_args);
{
Stop_Restart.Start();
string Output = Stop_Restart.StandardOutput.ReadToEnd();
string Errormessage = Stop_Restart.StandardError.ReadToEnd();
Stop_Restart.WaitForExit();
}

}


}
}

我在尝试捕获错误时添加了以下更改,但这并没有帮助我走得更远。我将在此处仅添加其中一个控件,但它已应用于每个按钮

private void Btn_stop_restart_Click(object sender, EventArgs e)
{
try
{
Process Stop_Restart = new Process();
Stop_Restart.StartInfo.UseShellExecute = false;
Stop_Restart.StartInfo.RedirectStandardOutput = true;
Stop_Restart.StartInfo.RedirectStandardError = true;
Stop_Restart.StartInfo.RedirectStandardInput = true;
Stop_Restart.StartInfo.FileName = Programmability.psexecpath;
var cancel_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -a" };
Stop_Restart.StartInfo.Arguments = String.Join(" ", cancel_args);
using (var stopreader = Stop_Restart.StandardOutput)
{
Stop_Restart.BeginOutputReadLine();
Stop_Restart.Start();
string Output = Stop_Restart.StandardOutput.ReadToEnd();
string Errormessage = Stop_Restart.StandardError.ReadToEnd();
Stop_Restart.WaitForExit();
var StopConsoleOut = stopreader.ReadToEnd();
MessageBox.Show(StopConsoleOut, "Success", MessageBoxButtons.OK);
}
}
catch (Exception)
{
MessageBox.Show($"Running psexec failed as {Programmability.psexecpath}", "Error", MessageBoxButtons.OK);
throw;
}
}

我将根据您的"它在我的机器上工作"和"PSExec正在打开但无所事事"的信息来回答。

请记住,当第一次运行 PSExec 时,您需要根据 PSExec 的输出使用 -accepteula:

"这是该计划的第一次运行。您必须接受 EULA 才能 继续。使用 -accepteula 接受 EULA。

是否已在要将此表单应用程序部署到的计算机上完成此步骤?

编辑:

如果您已接受 EULA,则应尝试确定 var "错误消息"是什么。

我会 - 为了在您要部署到的计算机上进行故障排除 - 建议您做类似的事情

Restart_now.Start();
string Output = Restart_now.StandardOutput.ReadToEnd();
string Errormessage = Restart_now.StandardError.ReadToEnd();
MessageBox.Show(Errormessage);
Restart_now.WaitForExit();

这样,您就可以看到要将此应用部署到的设备上出现故障的内容(如果有的话(。您可能会发现 PSExec 命令由于多种原因(无法连接到远程 PC、权限不足等(未在他们的计算机上运行

最新更新