简体   繁体   中英

How to shutdown machine from ASP.NET

I have a machine running Windows Home Server v1 (WHS is based on Windows Server 2003 and is runningh IIS6) and on here I have a restful webservice running which I build in C# VS2008 (dotnet 3.5). From within the webservice I want to be able to; 1 Check that certain windows services are running; 2 Start certain windows services 3 Stop certain windows services 4 Reboot the machine 5 Shutdown the machine

For 1-3 then I am using impersonation to elevate the ASPNET user to the local administrator (it is only me running this on a local secure network) and then "ServiceController" to control the services. This works perfectly.

For for 4 & 5 I am having issues and can't get it to work.

If I use System.Diagnostics.Process to call the "shutdown.exe" command with the parameters "/s /f" then the process executes without any errors but does not do anything! No shutdown, no exception nothing and I can't work out why. I have tried setting the local admin username & password but it does not help, and the impersonate user call does not help.

My code

string shut_args = "/s /f /t 10";
Process process1 = new Process();

process1.StartInfo.FileName = "shutdown.exe";
process1.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
SecureString password = new SecureString();
foreach (char c in "mypassword")
     password.AppendChar(c);
process1.StartInfo.Password = password;
process1.StartInfo.Domain = "homeserver";
process1.StartInfo.UserName = "Administrator";
process1.StartInfo.Arguments = shut_args;

process1.StartInfo.CreateNoWindow = true;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;                                

impersonateValidUser();
process1.Start();

So instead I tried to use WMI (code taken from another post on here) but here I get an "Privilege not held" error when trying to call InvokeMethod

Mycode

ManagementBaseObject mboShutdown = null; ManagementClass mcWin32 = new ManagementClass    ("Win32_OperatingSystem"); mcWin32.Get();        
            mcWin32.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
            mcWin32.Scope.Options.Authentication = AuthenticationLevel.Connect;
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");        

            // Flag 1 means we want to shut down the system. Use "2" to reboot.        
            mboShutdownParams["Flags"] = "1";        
            mboShutdownParams["Reserved"] = "0";        
            foreach (ManagementObject manObj in mcWin32.GetInstances())        
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);        
            }

I have also been through all of my security settings for the ASPNET and NETWORK_SERVICE user and they have rights to shutdown the server, the WMI security settings are also set-up for these users. But I just can't figure out what is wrong.

Any ideas?

Cheers

Have you tried to run a simple batch file instead of starting up the shutdown process in ASP.NET ? The whole process is described here: remote shutdown/restart using ASP.NET

Old question, but I wanted to do this myself, and just figured out the answer. So try this:

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "shutdown";
startInfo.Arguments = "/s /t 5";
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();

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.

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