2011-08-06 46 views
4

我有一臺運行Windows Home Server v1(WHS基於Windows Server 2003並運行IIS6)的計算機,並且在這裏我有一個運行於C#VS2008中的靜態web服務(dotnet 3.5)。從我希望能夠的web服務內部; 1檢查某些Windows服務正在運行; 2啓動某些Windows服務 3個停止某些Windows服務 4重新啓動機器 5關閉機器如何從ASP.NET關閉計算機

1-3然後我使用模擬來提升ASPNET用戶在本地管理員(它只是我跑這在本地安全網絡上),然後是「ServiceController」來控制服務。這工作完美。

對於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

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安全設置搜索gs也爲這些用戶設置。但我無法弄清楚什麼是錯的。

任何想法?

乾杯

回答

3

您是否嘗試過運行一個簡單的批處理文件,而不是啓動ASP.NET中的關機過程? 整個過程在這裏描述:remote shutdown/restart using ASP.NET

+1

+1,因爲它看起來很有希望 –

+0

哇,叫bat文件工作!但我認爲這不是一個很好的解決方案,而是更多的解決方法,並且希望對代碼的錯誤發表任何意見。 –

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