[英]Piping in a file on the command-line using System.Diagnostics.Process
[英]Restart windows from asp.net page using command prompt and 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;
}
对于文件名和参数部分,我也对此进行了测试:
si.StartInfo.FileName = "shutdown.exe";
si.StartInfo.Arguments = "/r /f /t 0 /c \"" + UReason + "\" /d p:4:1";
从RUN命令直接使用文件名和参数实际上会重新启动PC,但是在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服务具有重启功能,我将不胜感激。
终于成功了...
有很多问题。 我在此处记录了该过程以供将来参考。 当心安全隐患。
多亏了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服务的一些安全方法(例如,使用子域和非标准端口),我利用Uwe Keim的A小C#类中的模拟用户来伪造上述方法内容,此代码:
try
{
using (new Impersonator("Admin Username", ".", "Admin Password"))
{
// Above method Content
.
.
.
}
}
catch (Exception ex)
{
return "Invalid Username or Password";
}
此代码检查提供的凭据在服务器上是否有效。 我没有在此应用程序上测试非管理用户,因为该服务器没有任何用户。
随时发表评论和纠正。 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.