2009-12-12 156 views
2

我正在使用WMI。我想訪問遠程系統信息。下面的代碼工作環回或本地主機上,但是當我試圖訪問遠程計算機出現下面的異常錯誤:「訪問被拒絕」WMI異常

Access is denied. (Exception from HRESULT:0X8005(E_ACCESSDENIED))

當開關2個系統之間使用。

The RPC server Is unavailable. (Exception from HRESULT: 0x800706BA)

當兩個系統是直接連接。在兩個系統上


OS:Windows服務包2.
防火牆=阻塞。
遠程過程服務=正在運行。

工具:.NET Visual Studio 2008的C#

代碼:

try 
{ 
    ConnectionOptions _Options = new ConnectionOptions(); 
    ManagementPath _Path = new ManagementPath(s); 

    ManagementScope _Scope = new ManagementScope(_Path, _Options); 
    _Scope.Connect(); 
    ManagementObjectSearcher srcd = new ManagementObjectSearcher("select * from Win32_DisplayConfiguration"); 
    foreach (ManagementObject obj in srcd.Get()) 
    { 
     //listBox5.Items.Add(obj.Properties.ToString()); 
     foreach (PropertyData aProperty in obj.Properties) 
     { 
      listBox1.Items.Add(aProperty.Name.ToString() + " : " + aProperty.Value); 
     } 
    } 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 
+1

@dtb:感謝您的編輯。由於編輯使他們不再相關,我清除了評論並關閉了投票。 – 2009-12-12 21:26:32

+0

關於它的任何解決方案? – Kiquenet 2012-02-10 12:30:42

回答

0

這可能是很多事情,但一開始,你需要:

  • 公開賽在RPC通信防火牆
  • 啓用遠程rpc呼叫

見:http://support.microsoft.com/kb/895085(雖然這涵蓋了一個稍微不同的問題的決議是相關的),如果你想查詢中使用您所創建的管理範圍,你應該使用它的構造函數的另一個重載

+0

tnkz親愛的, 防火牆被阻止和rpc WMI和遠程訪問服務正在運行 – ADIL 2009-12-22 15:25:30

0

。我懷疑,你之間漏掉了代碼? 如果您在ConnectionOptions中使用過憑據,那麼ManagementObjectServer將不會使用它們。

嘗試:

ManagementObjectSearcher srcd; 
srcd = new ManagementObjectSearcher 
(
    _Scope, new ObjectQuery("select * from Win32_DisplayConfiguration") 
); 

尋找它在MSDN參考。

+0

感謝兄弟爲你的回覆...我也嘗試用戶憑證,但即時通訊面臨同樣的問題.. – ADIL 2009-12-22 15:16:34

+0

我再次發送代碼 – ADIL 2009-12-22 15:17:09

3

注:如果不指定憑據,運行用戶的憑據將被使用,因此這些都必須有效訪問遠程計算機,通常,這個賬戶必須是在遠程箱管理員(不對於所有對象,但只是可以肯定)。

如果您使用在兩臺計算機上都有效的域帳戶登錄,它可以立即使用。

如果您不在域環境中,只需指定憑據即可。

試試這個:

 
ConnectionOptions co = new ConnectionOptions(); 
co.Impersonation = ImpersonationLevel.Impersonate; 
co.Authentication = AuthenticationLevel.Packet; 
co.Timeout = new TimeSpan(0, 0, 30); 
co.EnablePrivileges = true; 
co.Username = "\\"; 
co.Password = ""; 

ManagementPath mp = new ManagementPath(); 
mp.NamespacePath = @"\root\cimv2"; 
mp.Server = "";    ///Regard this!!!! 

ManagementScope ms = new ManagementScope(mp, co); 
ms.Connect(); 

ManagementObjectSearcher srcd; 
srcd = new ManagementObjectSearcher 
(
    ms, new ObjectQuery("select * from Win32_DisplayConfiguration") 
); 

這是工作所有的時間對我來說。

從我的角度來看,出現該問題,因爲你是不是在你的ManagementPath指定遠程計算機。使用默認值創建的ManagementPath始終指向本地計算機。如果您只是指定憑據到本地計算機,這是不允許的,總是失敗。

BR - mabra

+0

我可以查詢此通過做srcd.get()但讀取任何東西(即使計數屬性)失敗與ManagementException,告訴我只有'無效的類'沒有內部異常... – Lorgarn 2014-11-04 13:32:42

0

如SO質疑asp classic - Access Denied errors accessing IIS WMI provider from ASP描述這可能是同樣的問題。

正如我在回答上述問題時所解釋的那樣,我在服務器上檢查了事件日誌(「Windows日誌」),我試圖通過WMI遠程訪問IIS,並且我發現用以下文本的事件:

Access to the root\WebAdministration namespace was denied because the namespace is marked with RequiresEncryption but the script or application attempted to connect to this namespace with an authentication level below Pkt_Privacy. Change the authentication level to Pkt_Privacy and run the script or application again.

The answer通過@mabra提出這個問題納入相關的靈感。以下是我添加的示例C#代碼,似乎爲我解決了這個問題:

ConnectionOptions options = new ConnectionOptions(); 
options.Authentication = AuthenticationLevel.PacketPrivacy; 
ManagementScope managementScope = new ManagementScope(@"\\remote-server\root\WebAdministration", options); 
// ... 
相關問題