简体   繁体   English

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

[英]WMI query in VB.NET - access denied

I am getting access denied when running the below code with alternate credentials ('Access is denied (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). 使用备用凭据运行以下代码时,我被拒绝访问(“访问被拒绝(HRESULT的异常:0x80070005(E_ACCESSDENIED))。

If I run the whole program under standard credentials, then pass an Administrator username & password into the WMI connection options, then I get access denied. 如果我以标准凭据运行整个程序,然后将管理员用户名和密码传递到WMI连接选项中,那么访问将被拒绝。 If however I right-click on the program and choose "RunAs" and put in an Administrator username & password (without passing credentials to the WMI options) then it works! 但是,如果我右键单击该程序并选择“ RunAs”,然后输入管理员用户名和密码(不将凭据传递给WMI选项),那么它将起作用! I gather from this that the account has the required privileges and that all the required ports are open so I don't believe this is a DCOM issue. 我从中得知该帐户具有必需的特权,并且所有必需的端口都已打开,因此我不认为这是DCOM问题。

I have also tried the wbemtest program and can connect to the remote PC just by entering username and password. 我还尝试了wbemtest程序,只需输入用户名和密码即可连接到远程PC。 I can always connect no matter which options I choose for impersonation & authentication level. 无论为模拟和身份验证级别选择哪个选项,我都可以始终连接。 In the program, I have experimented by putting various parameters for these options (see the commented lines) and have also tried the .EnablePrivileges option but no combination of these will make the program work. 在程序中,我通过为这些选项放置各种参数进行了实验(请参见注释行),还尝试了.EnablePrivileges选项,但这些方法的任何组合都无法使程序正常工作。 What am I missing here? 我在这里想念什么?

    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

I will assume the WMI configuration is correct in the remote PC 我将假定远程PC中的WMI配置正确

in your code you need to pass the "myManagementScope" as object and create query object and pass it both to object searcher. 在您的代码中,您需要将“ myManagementScope”作为对象传递,并创建查询对象,然后将两者都传递给对象搜索器。

The below code in your code didn't pass the credentials to the OjectSearcher, change the below code 您代码中的以下代码未将凭据传递给OjectSearcher,请更改以下代码

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

to

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

    Dim myObjectSearcher As New ManagementObjectSearcher( _
        myManagementScope, x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM