簡體   English   中英

“訪問被拒絕”WMI異常

[英]“Access is denied” Exception with WMI

我正在研究WMI。 我想訪問遠程系統信息。 以下代碼適用於環回或本地主機,但是當我嘗試訪問遠程計算機時,它顯示以下異常錯誤:

訪問被拒絕。 (HRESULT異常:0X8005(E_ACCESSDENIED))

在兩個系統之間使用開關時。

RPC服務器不可用。 (HRESULT異常:0x800706BA)

當兩個系統直接連接時。


兩個系統上的操作系統:Windows Service Pack 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);
}

注意:如果您未指定憑據,則將使用正在運行的用戶的憑據,因此這些憑證必須有效才能訪問遠程計算機,並且通常,此帳戶必須是遠程計算機上的管理員(不適用於所有對象,但只是為了確定)。

如果您使用在兩台計算機上都有效的域帳戶登錄,則可以開箱即用。

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

試試這個:

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

這可能是很多事情,但首先你需要:

  • 在防火牆中打開RPC流量
  • 啟用遠程rpc調用

請參閱: http//support.microsoft.com/kb/895085 (雖然這涉及一個稍微不同的問題,但解決方案是相關的)

如果您希望查詢使用您創建的ManagementScope,則應使用其構造函數的另一個重載。 我懷疑,你之間省略了代碼? 如果您已在ConnectionOptions中使用過憑據,則ManagementObjectServer將不會使用它們。

嘗試:

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

在MSDN參考中查找它。

這可能與SO問題asp經典中描述的問題相同- 從ASP訪問IIS WMI提供程序的訪問被拒絕錯誤

正如我在回答上述問題時所解釋的,我檢查了我試圖通過WMI遠程訪問IIS的服務器上的事件日志(“Windows Logs”),並且看到我發現了一個事件以下文字:

無法訪問root \\ WebAdministration命名空間,因為命名空間標記為RequiresEncryption,但腳本或應用程序嘗試使用低於Pkt_Privacy的身份驗證級別連接到此命名空間。 將身份驗證級別更改為Pkt_Privacy並再次運行腳本或應用程序。

@mabra為這個問題提供的答案包括相關的靈感。 這是我添加的示例C#代碼,似乎為我解決了這個問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM