2011-05-05 13 views
2

我正在嘗試在C#-MVC#2008 Express中創建一個小程序,它可以讓我遠程打開和關閉15臺電腦。打開它們很容易,但關閉它們似乎有點問題。使用system.management進行關機時訪問被拒絕

首先,我沒有域或SharePoint,只是Windows XP上的一個簡單的工作組。現在,我設法讓shutdown.exe工作,但我想必須有一個C#解決方案,所以經過一番搜索後,我發現一些使用system.management命名空間的代碼運行良好。

這兩種解決方案唯一的問題是,我需要登錄一個相同的管理員帳戶,好吧,讓我們說並非每個人與我一起工作是最科技的頭腦,所以想到讓他們使用管理員帳戶讓我感到緊張。

我能不能讓他們訪問該功能,但我發現下面的代碼:

void Shutdown() { 
    try 
    { 
     const string computerName = "PC05"; // computer name or IP address 
     ConnectionOptions options = new ConnectionOptions(); 
     options.EnablePrivileges = true; 
     // To connect to the remote computer using a different account, specify these values: 
     //options.Username = ""; 
     //options.Password = ""; 
     //options.Authority = "ntlmdomain:DOMAIN"; 

     //ManagementScope scope = new ManagementScope("\\\\" + computerName + "\\root\\cimv2", options); 
     ManagementScope scope = new ManagementScope(); 
     scope.Connect(); 
     SelectQuery query = new SelectQuery("Win32_OperatingSystem"); 
     ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
     foreach (ManagementObject os in searcher.Get()) 
     { 
      // Obtain in-parameters for the method 
      ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown"); 
      // Add the input parameters. 
      inParams["Flags"] = 2; 
      // Execute the method and obtain the return values. 
      ManagementBaseObject outParams = os.InvokeMethod("Win32Shutdown", inParams, null); 
     } 
    } 
    catch(ManagementException err) 
    { 
    MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message); 
    } 
    catch(System.UnauthorizedAccessException unauthorizedErr) 
    { 
    MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message); 
    } 
} 

但我不斷收到訪問被拒絕錯誤,當我嘗試使用它。

「訪問被拒絕(異常來自HRESULT:0X80070005(E_ACCESSDENIED))。」} {System.Exception的} System.UnauthorizedAccessException的

我試着取消註釋只是用戶名和密碼(與通和用戶名我知道的管理員帳戶是正確的)也是取消評論的權力。我用:

options.Impersonation = ImpersonationLevel.Impersonate; 
options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy; 

但似乎沒有任何工作。我不知道是否需要爲此設置特殊設置,但正如我所說的,如果我登錄到另一臺計算機上使用的管理員帳戶,則可以進行連接和關閉。我目前正在另一個管理員帳戶中進行測試。

我已閱讀:

http://msdn.microsoft.com/en-us/library/aa393613(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa393266(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa389286(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa389290(VS.85).aspx(真的不完全得到這個)

也許這是隻允許在域,但我還沒有找到任何確認。我想避免不得不添加另一個帳戶,所以有什麼建議?

+0

什麼是錯的使用shutdown.exe的? – soandos 2011-05-05 19:04:47

+0

什麼也沒有,但正如我所提到的,我想要一個選項,我不必以管理員身份登錄。我還沒有找到允許有限帳戶訪問它的方法。 – Crazyluv 2011-05-05 19:43:49

回答

0

這裏有一個簡單的解決辦法,可能是您的情況非常有用(讓我知道,如果它是你可以使用的解決方案):

remote shutdown (*.bat script) without administrator rights

+0

我不確定這對我來說是最理想的選擇,但如果我找不到其他東西,我會試試看。 – Crazyluv 2011-05-09 19:10:28

2
ManagementBaseObject outParams = null; 
ManagementClass os = new ManagementClass("Win32_OperatingSystem"); 
os.Get(); 
os.Scope.Options.EnablePrivileges = true; // enables required security privilege. 
ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown"); 
inParams["Flags"] = "2"; // System reboot 
inParams["Reserved"] = "0"; 
foreach (ManagementObject mo in os.GetInstances()) 
    outParams = mo.InvokeMethod("Win32Shutdown", 
    inParams, null);