0

我正在尝试使用 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;
}

对于文件名和参数部分,我也对此进行了测试:

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

使用 RUN 命令中的文件名和参数实际上会重新启动电脑,但在 Web 服务上我收到此错误:

在服务器桌面上:应用程序无法正确初始化 (0xC0000142)。单击确定以终止应用程序。

在事件日志中我有这个:

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 

在 Web 应用程序上没有错误。

如果有人告诉我如何解决此问题并为 Web 服务提供重新启动功能,我将不胜感激。

4

1 回答 1

0

终于成功了……

有各种各样的问题。我在此处记录该过程以供将来参考。谨防安全风险。

感谢Wjdavis5,我将 AppPool Identity 更改为 Local System。

感谢从 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;
}

为了消除安全隐患,除了一些隐藏网站和Web服务的安全方法,例如使用子域和非标准端口,我在A small C# Class for impersonating a User by Uwe Keim中使用了impersonation,将上述方法内容包裹在这段代码:

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

此代码检查提供的凭据在服务器上是否有效。我没有在这个应用程序上测试非管理用户,因为这个服务器没有。

随时评论和纠正。问候

于 2013-12-03T00:09:46.197 回答