1

使用备用凭据运行以下代码时,我被拒绝访问('访问被拒绝(来自 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
4

1 回答 1

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)
于 2016-12-27T13:30:26.787 回答