Starting psexec process fails in windows form.

huangapple go评论70阅读模式
英文:

Starting psexec process fails in windows form

问题

I have translated the code part you provided as requested. Here it is:

所以,我已经创建了一个表单,允许我们的营销团队重新启动一台显示幻灯片/重要信息供访客查看的计算机。

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

该应用程序在我的计算机上在调试和构建中运行正常。
但在安装到其他人的计算机上时,似乎启动了psexec,然后没有做任何事情。

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

> 标准输出未被重定向

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

我的代码如下:

```csharp
// 你的C#代码在这里

我已翻译了你提供的代码部分,如果需要更多帮助或有其他问题,请告诉我。

英文:

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.

I added some catch exceptions in at which point it shows me the following error:

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

答案1

得分: 0

I'm going to answer based on your information of "it works on my machine" and "PSExec is opening but not doing anything".

Remember that when PSExec is run for the first time you are required to use -accepteula as per PSExec's output:

"This is the first run of this program. You must accept EULA to continue. Use -accepteula to accept 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.

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)

英文:

I'm going to answer based on your information of "it works on my machine" and "PSExec is opening but not doing anything".

Remember that when PSExec is run for the first time you are required to use -accepteula as per PSExec's output:

> "This is the first run of this program. You must accept EULA to
> continue. Use -accepteula to accept 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.

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)

huangapple
  • 本文由 发表于 2020年1月3日 23:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定