C# 使用命令提示符和System.Diagnostics.Process从asp.net页面重新启动windows
我正在尝试使用System.Diagnostics.Process从web服务内部重新启动windows server 2003C# 使用命令提示符和System.Diagnostics.Process从asp.net页面重新启动windows,c#,asp.net,windows-services,command-prompt,runcommand,C#,Asp.net,Windows Services,Command Prompt,Runcommand,我正在尝试使用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 (
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命令中的filename和argument实际上会重新启动电脑,但在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
过程信息:
进程ID:2676
进程名称:w3wp.exe
帐户名称:NT授权\网络服务
例外信息:
异常类型:HttpException
异常消息:请求超时。
请求信息:
请求URL:http://mywebsite.com/webservice.asmx
请求路径:/webservice.asmx
用户主机地址:
用户:
已验证:False
身份验证类型:
线程帐户名称:NT授权\网络服务
线程信息:
线程ID:7
线程帐户名称:NT授权\网络服务
是模仿:假
在Web应用程序上没有错误
如果有人告诉我如何解决此问题并为web服务提供重新启动功能,我将不胜感激。最终它成功了
有各种各样的问题。我将此过程记录在这里,以供将来参考。谨防安全风险
由于,我将AppPool标识更改为本地系统
由于,我从代码中删除了一些行:
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服务中重新启动服务器?w3wp.exe工作的过程,即您的应用程序池,应该有一个具有管理员权限的帐户。@Wjdavis5-因为我需要在不同的internet和extranet网站上提供一些服务,并具有重新启动服务器的权限。这很奇怪吗?@Deowalk谢谢你的回复。但最佳实践是什么?我检查了那个网络服务用户。我还可以选择将其更改为IWAN_u2;。我想将这些用户添加到administrators组可能会导致安全问题(请更正)。我尝试将其更改为IWAN用户,并在组策略编辑器中将该用户添加到Can Shutdown组中,但没有成功。你有什么建议?我建议运行一个测试,让应用程序池以本地管理员的身份在计算机上运行,看看它是否正常工作。然后开始从那里退出权限。这几乎是一个权限问题。
try
{
using (new Impersonator("Admin Username", ".", "Admin Password"))
{
// Above method Content
.
.
.
}
}
catch (Exception ex)
{
return "Invalid Username or Password";
}