Reputation: 11
I am getting access denied when running the below code with alternate credentials ('Access is denied (Exception from 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. 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! 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.
I have also tried the wbemtest program and can connect to the remote PC just by entering username and password. 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. 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
Upvotes: 1
Views: 3111
Reputation: 1
I will assume the WMI configuration is correct in the remote PC
in your code you need to pass the "myManagementScope" as object and create query object and pass it both to object searcher.
The below code in your code didn't pass the credentials to the OjectSearcher, change the below code
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)
Upvotes: 0