I'm trying to restart windows server 2003 from inside a web service using System.Diagnostics.Process.
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:
On server desktop: The application fails to initialize properly (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.
I appreciate if somebody tell me how can I fix this problem and give restart ability to a web service.
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.
Thanks to Running a batch file from an ASP .NET page , I removed some lines from code:
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.