[英]Starting psexec process fails in windows form
因此,我创建了一个表单,允许我们的营销团队重新启动计算机,为访客显示幻灯片/重要信息等
计划是将其部署到其他用户计算机,然后他们可以执行以下三项任务之一: 立即重启 定时/计划重启 取消定时/计划重启
该应用程序在我的计算机上运行良好,在调试和构建中都很好。 在其他人的计算机上安装时,它似乎正在启动 psexec 但随后什么也没做。
我添加了一些 catch 异常,此时它显示了以下错误:
标准没有被重定向
但是,在对此进行研究时,我似乎已经拥有大多数人建议解决此问题的代码。
我的代码如下:
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 "Errormessage" 是什么。
我会 - 为了在您部署的机器上进行故障排除 - 建议您执行类似的操作
Restart_now.Start();
string Output = Restart_now.StandardOutput.ReadToEnd();
string Errormessage = Restart_now.StandardError.ReadToEnd();
MessageBox.Show(Errormessage);
Restart_now.WaitForExit();
这样您就可以看到您要部署此应用程序的设备上发生了什么故障(如果有)。 您可能会发现由于多种原因(无法连接到远程 PC、权限不足等),他们的机器上没有运行 PSExec 命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.