【发布时间】:2013-05-04 12:07:09
【问题描述】:
使用备用凭据运行以下代码时,访问被拒绝('访问被拒绝(来自 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
【问题讨论】:
-
嗨!为您删除 ConnectOptions 的 cmets,这确实是必需的;此外,为连接指定 ".Authority = "NTLMDOMAIN:" + computerName"。 [你试过“dom\user”格式吗??]。