VB.NET中的WMI查询 - 访问被拒绝

时间:2013-05-04 12:07:10

标签: .net vb.net wmi

我在使用备用凭据运行以下代码时拒绝访问(“拒绝访问(HRESULT异常:0x80070005(E_ACCESSDENIED))。

如果我使用标准凭据运行整个程序,则传递管理员用户名&密码进入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

1 个答案:

答案 0 :(得分:0)

我将假设远程PC中的WMI配置正确

在您的代码中,您需要将“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)