Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET中的WMI查询-拒绝访问_.net_Vb.net_Wmi - Fatal编程技术网

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

VB.NET中的WMI查询-拒绝访问,.net,vb.net,wmi,.net,Vb.net,Wmi,使用备用凭据运行以下代码时,我的访问被拒绝(“访问被拒绝(来自HRESULT的异常:0x80070005(E_ACCESSDENIED)) 如果我使用标准凭据运行整个程序,然后将管理员用户名和密码传递到WMI连接选项,则访问被拒绝。但是,如果我右键单击程序并选择“RunAs”,然后输入管理员用户名和密码(不将凭据传递到WMI选项)然后它就可以工作了!我由此推断,该帐户具有所需的权限,并且所有所需的端口都已打开,因此我不认为这是DCOM问题 我还尝试了wbemtest程序,只需输入用户名和密码即可

使用备用凭据运行以下代码时,我的访问被拒绝(“访问被拒绝(来自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
Sub-Main()
将myConnectionOptions设置为新System.Management.ConnectionOptions
使用myConnectionOptions
'.EnablePrivileges=True
'.Impersonation=System.Management.ImpersonationLevel.Impersonate
'.Authentication=System.Management.AuthenticationLevel.PacketPrivacy
如果是TextBoxUserName.Text“”,则
.Username=TextBoxUserName.Text
.Password=TextBoxPassword.Text
如果结束
以
"建立联系",
尝试
将myManagementScope设置为System.Management.ManagementScope
myManagementScope=新系统。管理。管理范围(_
“\\”&TextBoxComputerName.Text&“\root\cimv2”,myConnectionOptions)
'连接到WMI命名空间
myManagementScope.Connect()
Dim myObjectSearcher作为新的ManagementObjectSearcher(_
myManagementScope.Path.ToString,“从Win32\U计算机系统中选择*)
将myCollection设置为ManagementObjectCollection
将myObject设置为ManagementObject
'执行查询
myCollection=myObjectSearcher.Get()
对于myCollection中的每个myObject
如果myObject.GetPropertyValue(“用户名”)为空,则
MsgBox(“Ctrl-Alt-Del”)
其他的
MsgBox(myObject.GetPropertyValue(“用户名”).ToString)
如果结束
下一个
捕获e作为例外
MsgBox(“\u连接错误”和e.Message)
结束尝试
端接头

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

在代码中,您需要将“myManagementScope”作为对象传递,创建查询对象并将其传递给对象搜索器

您代码中的以下代码未将凭据传递给OjectSearcher,请更改以下代码

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


您好!请删除您的注释ConnectOptions,这确实是必需的;此外,请为连接指定“.Authority=”NTLMDOMAIN:“+computerName”。[您是否尝试过“dom\user”格式??]。
    Dim x ObjectQuery
    x = New ObjectQuery("Select * From Win32_ComputerSystem")

    Dim myObjectSearcher As New ManagementObjectSearcher( _
        myManagementScope, x)