简体   繁体   English

以 Windows 形式启动 psexec 进程失败

[英]Starting psexec process fails in windows form

So I have created a form to allow our Marketing team to restart a computer that displays a slideshow/important information for visitors etc因此,我创建了一个表单,允许我们的营销团队重新启动计算机,为访客显示幻灯片/重要信息等

The plan is to deploy this to other user comptuers and then they can do one of the three following tasks: Restart now Timed/Scheduled restart Cancel Timed/Scheduled restart计划是将其部署到其他用户计算机,然后他们可以执行以下三项任务之一: 立即重启 定时/计划重启 取消定时/计划重启

The application works fine on my computer just fine in debug and in build.该应用程序在我的计算机上运行良好,在调试和构建中都很好。 When installing on other people's computers it seems as if it is launching psexec but then not doing anything.在其他人的计算机上安装时,它似乎正在启动 psexec 但随后什么也没做。

I added some catch exceptions in at which point it shows me the following error:我添加了一些 catch 异常,此时它显示了以下错误:

standardout has not been redirected标准没有被重定向

However when researching into this I seem to already have the code most people suggest to fix this issue.但是,在对此进行研究时,我似乎已经拥有大多数人建议解决此问题的代码。

My code is as below:我的代码如下:

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


    }




      }
}

I added the following changes when trying to catch the error but this has not helped me get any further.我在尝试捕获错误时添加了以下更改,但这并没有帮助我进一步了解。 I will add just one of the controls for show here but it has been applied to each button我将在此处仅添加一个用于显示的控件,但它已应用于每个按钮

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;
        }
    }

I'm going to answer based on your information of "it works on my machine" and "PSExec is opening but not doing anything".我将根据您的“它在我的机器上工作”和“PSExec 正在打开但没有做任何事情”的信息来回答。

Remember that when PSExec is run for the first time you are required to use -accepteula as per PSExec's output:请记住,当第一次运行 PSExec 时,您需要根据 PSExec 的输出使用 -accepteula:

"This is the first run of this program. You must accept EULA to continue. Use -accepteula to accept EULA." “这是该程序的第一次运行。您必须接受 EULA 才能继续。使用 -accepteula 接受 EULA。”

Have you completed this step on the machines which you are deploying this form app to?您是否在要部署此表单应用程序的计算机上完成了此步骤?

Edit:编辑:

If you have accepted the EULA, then you should try to identify what var "Errormessage" is.如果您已接受 EULA,那么您应该尝试确定 var "Errormessage" 是什么。

I would - for troubleshooting on the machines you are deploying to sake - recommend that you do something like我会 - 为了在您部署的机器上进行故障排除 - 建议您执行类似的操作

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

So that you can see what is failing, if anything, on the device you are deploying this app to.这样您就可以看到您要部署此应用程序的设备上发生了什么故障(如果有)。 You may find that the PSExec command is not running on their machines for a number of reasons (cannot connect to remote PC, insufficient permissions, etc)您可能会发现由于多种原因(无法连接到远程 PC、权限不足等),他们的机器上没有运行 PSExec 命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM