我有一台运行Windows Home Server v1的机器(WHS基于Windows Server 2003并运行IIS6),在这里我运行了一个安静的Web服务,我在C#VS2008(dotnet 3.5)中构建。从我想要的web服务中; 1检查某些Windows服务是否正在运行; 2启动某些Windows服务 3停止某些Windows服务 4重新启动机器 5关闭机器
对于1-3然后我使用模拟将ASPNET用户提升到本地管理员(只有我在本地安全网络上运行它)然后“ServiceController”来控制服务。这非常有效。
for for 4& 5我遇到了问题,无法让它发挥作用。
如果我使用System.Diagnostics.Process调用带有参数“/ s / f”的“shutdown.exe”命令,则该过程执行时没有任何错误但不执行任何操作!没有关机,没有例外,我无法解决原因。我尝试过设置本地管理员用户名&密码,但它没有帮助,模拟用户调用没有帮助。
我的代码
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(从这里的另一篇文章中获取的代码)但是在这里我尝试调用InvokeMethod时出现“Privilege not held”错误
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);
}
我还经历了ASPNET和NETWORK_SERVICE用户的所有安全设置,他们有权关闭服务器,还为这些用户设置了WMI安全设置。但我无法弄清楚出了什么问题。
有什么想法吗?
干杯
答案 0 :(得分:3)
您是否尝试过运行简单的批处理文件而不是在ASP.NET中启动关闭过程? 整个过程在此处描述:remote shutdown/restart using ASP.NET
答案 1 :(得分:0)
老问题,但我想自己这样做,只是想出答案。所以试试这个:
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();