简体   繁体   English

使用命令提示符和System.Diagnostics.Process从asp.net页面重新启动Windows

[英]Restart windows from asp.net page using command prompt and System.Diagnostics.Process

I'm trying to restart windows server 2003 from inside a web service using System.Diagnostics.Process. 我正在尝试使用System.Diagnostics.Process从Web服务内部重新启动Windows Server 2003。

public static string Dorestart()
{
  var si = new Process();

  si.StartInfo.UserName = "administrator"; // Credentials of administrator user

  var sc = new SecureString();
  foreach (char c in "AdminPassword")
  {
    sc.AppendChar(c);
  }
  si.StartInfo.Password = sc;

  si.StartInfo.UseShellExecute = false;

  si.StartInfo.FileName = "cmd.exe";
  si.StartInfo.Arguments = "\"c:\\windows\\system32\\shutdown.exe\" -r -f -t 0 -c \"Restart Reason\" -d p:4:1";
  si.StartInfo.CreateNoWindow = true;

  string res = "";
  try
  {
    si.Start();
    si.WaitForExit();

    res = "Minor Job done... wait 2 minutes to complete action";
  }
  catch (Exception ex)
  {
    res= ex.Message;
  }

  si.Close();
  si.Dispose();

  return res;
}

for file name and argument part I also tested this: 对于文件名和参数部分,我也对此进行了测试:

si.StartInfo.FileName = "shutdown.exe";
si.StartInfo.Arguments = "/r /f /t 0 /c \"" + UReason + "\" /d p:4:1";

using filename and argument right from RUN command actually restarts the pc but on web service I get this error: 从RUN命令直接使用文件名和参数实际上会重新启动PC,但是在Web服务上却出现此错误:

On server desktop: The application fails to initialize properly (0xC0000142). 在服务器桌面上:应用程序无法正确初始化(0xC0000142)。 Click on ok to terminate application. 单击确定终止应用程序。

In event log I have this: 在事件日志中,我有这个:

Process information: 
Process ID: 2676 
Process name: w3wp.exe 
Account name: NT AUTHORITY\NETWORK SERVICE 

Exception information: 
Exception type: HttpException 
Exception message: Request timed out. 

Request information: 
Request URL: http://mywebsite.com/webservice.asmx 
Request path: /webservice.asmx 
User host address: <IP Address> 
User:  
Is authenticated: False 
Authentication Type:  
Thread account name: NT AUTHORITY\NETWORK SERVICE 

Thread information: 
Thread ID: 7 
Thread account name: NT AUTHORITY\NETWORK SERVICE 
Is impersonating: False 

On Web Application there is no error. 在Web应用程序上没有错误。

I appreciate if somebody tell me how can I fix this problem and give restart ability to a web service. 如果有人告诉我如何解决此问题并使Web服务具有重启功能,我将不胜感激。

at last it worked... 终于成功了...

There were a mixture of problems. 有很多问题。 I document the process here for future reference. 我在此处记录了该过程以供将来参考。 Beware of security risks. 当心安全隐患。

Thanks to Wjdavis5 , I changed AppPool Identity to Local System. 多亏了Wjdavis5 ,我将AppPool Identity更改为Local System。

Thanks to Running a batch file from an ASP .NET page , I removed some lines from code: 感谢从ASP .NET页面运行批处理文件 ,我从代码中删除了几行:

public string DoJob()
{
  var si = new Process
  {
    StartInfo =
    {
      FileName = "shutdown.exe",
      Arguments = "-r -f -t 0 -c "Shutdown Reason" -d p:4:1",
      WorkingDirectory = "c:\\windows\\system32\\"
    }
  };

  string res;
  try
  {
    si.Start();
    si.WaitForExit();

    res = "<br />Minor Job done... wait 2 minutes to complete action<br />You can now close this window";
  }
  catch (Exception ex)
  {
    res = ex.Message;
  }

  si.Close();
  si.Dispose();
  return res;
}

To eliminate security risks, alongside with some security approaches in hiding website and web service, such as using subdomain and non-standard port, I made use of impersonation in A small C# Class for impersonating a User by Uwe Keim the above method content wrapped in this code: 为了消除安全风险,以及隐藏网站和Web服务的一些安全方法(例如,使用子域和非标准端口),我利用Uwe KeimA小C#类中的模拟用户来伪造上述方法内容,此代码:

try
{
  using (new Impersonator("Admin Username", ".", "Admin Password"))
  {
    // Above method Content
    .
    .
    .
  }
}
catch (Exception ex)
{
    return "Invalid Username or Password";
}

this code checks if provided credential is valid on server. 此代码检查提供的凭据在服务器上是否有效。 I did not test non-administrative users on this application because this server does not have any. 我没有在此应用程序上测试非管理用户,因为该服务器没有任何用户。

feel free to comment and correct. 随时发表评论和纠正。 Regards 问候

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

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