2013-05-04 80 views
1

我正在運行下面的代碼備用憑據時(「訪問被拒絕(異常來自HRESULT訪問被拒絕:0X80070005(E_ACCESSDENIED))WMI查詢 - 訪問被拒絕

如果我運行的整體。程序在標準的憑據下,然後傳遞一個管理員用戶名&密碼到WMI連接選項,然後我得到訪問被拒絕。如果然後我右鍵點擊該程序並選擇「RunAs」並輸入一個管理員用戶名&密碼到WMI選項),那麼它的工作原理!我從中得知該帳戶具有所需的權限,並且所有必需的端口都是開放的,所以我不相信這是DCOM問題。

我也嘗試過wbemtest程序,只需輸入用戶名和密碼即可連接到遠程PC。我總是可以連接,無論我選擇哪個模擬&身份驗證級別。在程序中,我通過爲這些選項設置各種參數進行了實驗(請參閱註釋行),並嘗試了.EnablePrivileges選項,但這些選項的組合不會使程序正常工作。我在這裏錯過了什麼?

Sub Main() 
    Dim myConnectionOptions As New System.Management.ConnectionOptions 
    With myConnectionOptions 
     '.EnablePrivileges = True 
     '.Impersonation = System.Management.ImpersonationLevel.Impersonate 
     '.Authentication = System.Management.AuthenticationLevel.PacketPrivacy 
     If TextBoxUserName.Text <> "" Then 
      .Username = TextBoxUserName.Text 
      .Password = TextBoxPassword.Text 
     End If 
    End With 
    'Establish connection 
    Try 
     Dim myManagementScope As System.Management.ManagementScope 
     myManagementScope = New System.Management.ManagementScope(_ 
      "\\" & TextBoxComputerName.Text & "\root\cimv2", myConnectionOptions) 
     'Connect to WMI namespace 
     myManagementScope.Connect() 
     Dim myObjectSearcher As New ManagementObjectSearcher(_ 
      myManagementScope.Path.ToString, "Select * From Win32_ComputerSystem") 
     Dim myCollection As ManagementObjectCollection 
     Dim myObject As ManagementObject 
     'Execute query 
     myCollection = myObjectSearcher.Get() 
     For Each myObject In myCollection 
      If myObject.GetPropertyValue("UserName") Is Nothing Then 
       MsgBox("Ctrl-Alt-Del") 
      Else 
       MsgBox(myObject.GetPropertyValue("UserName").ToString) 
      End If 
     Next 
    Catch e As Exception 
     MsgBox("_Connection Error" & e.Message) 
    End Try 
End Sub 
+0

嗨!刪除你的評論ConnectOptions,這真的是必需的;此外,爲連接指定「.Authority =」NTLMDOMAIN:「+ computerName」。 [你是否嘗試過「dom \ user」格式??]。 – mabra 2013-05-05 21:20:34

回答

0

我將承擔WMI配置是在你的代碼遠程PC

正確的,你需要通過「myManagementScope」爲對象,並創建查詢對象,並把它傳遞既反對搜索。

下面的代碼在你的代碼沒有憑據傳遞到OjectSearcher,下面的代碼

Dim myObjectSearcher As New ManagementObjectSearcher(_ 
     myManagementScope.Path.ToString, "Select * From Win32_ComputerSystem") 

改變

Dim x ObjectQuery 
    x = New ObjectQuery("Select * From Win32_ComputerSystem") 

    Dim myObjectSearcher As New ManagementObjectSearcher(_ 
     myManagementScope, x)