如何关闭机器从ASP.NET [英] How to shutdown machine from ASP.NET

查看:101
本文介绍了如何关闭机器从ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行Windows Home Server的V1(WHS是基于Windows Server 2003上,是runningh IIS6)的计算机上,并在这里我有一个RESTful Web服务的运行,我建在C#中VS2008(DOTNET 3.5)。从我希望能够在Web服务中;
1检查某些Windows服务正在运行;
2启动某些Windows服务
3停止某些Windows服务
4重新启动机器
5关闭本机

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

有关1-3然后我使用模拟来提升ASPNET用户在本地管理员(这是只有我一个本地安全网络上运行这一点),然后ServiceController的来控制服务。这完美的作品。

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.

有关为4和5我有问题并不能得到它的工作。

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

如果我使用的System.Diagnostics.Process称之为shutdown.exe的带参数的命令/ S / F,则执行过程中没有任何错误,但不会做任何事情!不关机,也不例外什么,我不能明白为什么。我曾尝试设置本地管理员的用户名和放大器;密码,但它并没有帮助,而模拟用户的呼叫没有帮助。

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.

我的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();

所以不是我试图用WMI(从这里另一职务采取code)但在这里我得到一个特权不举办试图调用时,InvokeMethod时误差

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

我的code

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);        
            }

我也一直通过所有为ASPNET和NETWORK_SERVICE用户我的安全设置,他们有权关闭服务器,则WMI安全设置也设置了为这些用户。但我就是无法弄清楚什么是错的。

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.

任何想法?

干杯

推荐答案

您是否尝试过运行一个简单的批处理文件,而不是启动ASP.NET中的关机过程?
整个过程描述如下:<一href=\"http://www.c-sharpcorner.com/UploadFile/yuanwang200409/RemoteRestartWindows09252006141003PM/RemoteRestartWindows.aspx\"相对=nofollow>远程关机/重启使用ASP.NET

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

这篇关于如何关闭机器从ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆