שתף באמצעות


ManagementScope.Connect giving an Error "Access Denied (Exception from HRESULT: =0x80070005 (E_ACCESSDENIED))]"

Question

Monday, June 29, 2009 2:38 PM

Hi friends...

I am trying to connect to a remote machine through a vb.net application and want to retrieve the system information using WMI Classes. But the source line (scope.Connect()) as below generates an error "Access Denied HRESULT...."

 Dim options As ConnectionOptions = New ConnectionOptions
 options.Username = Nothing
 options.Password = Nothing
 options.Impersonation = ImpersonationLevel.Impersonate
 options.Authentication = AuthenticationLevel.Packet
 options.EnablePrivileges = True

 Dim path As ManagementPath = New Managementath("\\192.168.0.2\root\cimv2")
 Dim scope As ManagementScope = New ManagementScope(path, options)
 Try
      scope.Connect()
 Catch ex As Exception
      MsgBox(ex.Message.ToString)
 End Try

I have tried following...
Given host/remote machine's name correct
Tried IP Address and also the host name
Disabled the antivirus and firewall on both machines

Thank You
Abhijit


A vb.net developer

All replies (5)

Tuesday, July 7, 2009 8:01 AM ✅Answered

Hi Abhijit,

Your code is still OK.

Error message "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)".
You experience such issue when you accee a remote computer via WMI. Usually the RPC Server Unavailable issue is related to firewall.
(PRC is for Remote Procedure Call)

Here are two suggestions for you to fix it.

  1. On the remote server "192.168.0.2" side, run the following command line:
        Netsh firewall set service RemoteAdmin
  2. On the remote server "192.168.0.2" side, disable the firewall.

Also check this article for some instruction:
http://www.aspdeveloper.net/tiki-index.php?page=FirewallWMIandRPC

Best regards,
Martin Xie

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


Thursday, July 2, 2009 9:57 AM

Hi Abhijit,

Your code is OK.

The error "Access is denied Exception from HRESULT: 0x80070005" indicates the requested user has no access to remote machine via WMI service.

You need to add "Remote Access" permission to ASPNET account on WMI component service.

Walkthrough: Do the following settings on remote server "192.168.0.2" side:

  • Run dcomcnfg command (via Start menu -> Run dialog) to bring up Component Services window.
  • Expand nodes: Component Services -> Computers -> My Computer > DCOM Config
  • Scroll down list, right-click Windows Management and Instrumentation, and select Properties, then "Management and Instrumentation Properties" window will be displayed.
  • Select the Security tab, then select "Customize" radio button to activate the Edit button, then you will see such two permission options ("Local Access" , "Remote Access") for each account.
  • Add the ASPNET account, and assign the "Remote Access" permission.   
  • Reboot the server.

Here are two threads for you to check, which discussed the similar issues to yours: 
http://forums.asp.net/t/1391976.aspx
http://social.msdn.microsoft.com/Forums/en-US/vsreportcontrols/thread/26accc30-9cfb-4d86-9c27-780f51929ecb

Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


Thursday, July 2, 2009 10:03 AM

By the way, you can check this code sample for reference:
How to connect to remote computer and check available space via Windows Management Instrumentation (WMI) service in VB.NET?
http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/2c8d1aa2-8736-472f-a1e6-12ad92a98d8d

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


Saturday, July 4, 2009 2:35 PM

Hi Martin Xie Sir...

As you said, I have added "Remote Access" permission to ASPNET account on WMI component service and rebooted the machine. But it is not working. It is still giving the same message.

And some times it gives the error message "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)". All with the same source code .

I am using this in a VB.NET windows application.

This is the actual class file  being used....

Public Class HardDrive
    Private htbl() As Hashtable = Nothing
    Private hdCollection As New ArrayList()

    Public Sub New()
        Dim options As ConnectionOptions = New ConnectionOptions
        'options.Username = System.Environment.UserName
        options.Impersonation = ImpersonationLevel.Impersonate
        options.Authentication = AuthenticationLevel.Default
        options.EnablePrivileges = True
        Dim path As ManagementPath = New ManagementPath("\\192.168.0.2\root\cimv2")
        Dim scope As ManagementScope = New ManagementScope(path, options)
        Dim q As New Management.ObjectQuery("SELECT * FROM Win32_DiskDrive")
        Dim searcher As New ManagementObjectSearcher(scope, q)
        'Dim searcher As New ManagementObjectSearcher("\\192.168.0.2\root\cimv2", "SELECT * FROM Win32_DiskDrive")
        Dim counter As Integer = 0
        htbl = New Hashtable(searcher.Get().Count - 1) {}
        For Each wmi_HD As ManagementObject In searcher.Get()
            Dim hd As New HardDriveInfo()
            hdCollection.Add(hd)
            htbl(counter) = New Hashtable()
            For Each pd As System.Management.PropertyData In wmi_HD.Properties
                htbl(counter).Add(pd.Name, pd.Value)
            Next pd
            'GET THE HARD DRIVE MODEL
            Try : hd.Model = htbl(counter)("Model").ToString() : Catch : End Try
            'GET THE HARD DRIVE MEDIA TYPE
            Try : hd.Type = htbl(counter)("MediaType").ToString() : Catch : End Try
            counter += 1
        Next wmi_HD
        q = New Management.ObjectQuery("SELECT * FROM Win32_PhysicalMedia")
        searcher = New ManagementObjectSearcher(scope, q)
        'searcher = New ManagementObjectSearcher("\\192.168.0.2\root\cimv2", "SELECT * FROM Win32_PhysicalMedia")
        counter = 0
        For Each wmi_HD As ManagementObject In searcher.Get()
            Dim hd As HardDriveInfo = CType(hdCollection(counter), HardDriveInfo)
            For Each pd As System.Management.PropertyData In wmi_HD.Properties
                If (Not htbl(counter).Contains(pd.Name)) Then
                    htbl(counter).Add(pd.Name, pd.Value)
                End If
            Next pd
            ' GET THE HARDWARE SERIAL No.
            Try : hd.SerialNo = htbl(counter)("SerialNumber").ToString() : Catch : End Try
            'GET THE HARD DRIVE SIGNATURE
            Try : hd.Signature = htbl(counter)("Signature").ToString() : Catch : End Try
            counter += 1
        Next wmi_HD
    End Sub
   
    Public Function GetAllDataAboutHarddrive() As Hashtable()
        Return htbl
    End Function
   
    Public Function GetHardDriveInfo() As ArrayList
        Return hdCollection
    End Function

End Class

Please help

Abhijit


A vb.net developer


Friday, December 4, 2009 3:32 PM

I'm getting the scenario that after a scope.connect to a remote machine the ASPNET account always ends up getting locked out on the remote machine.  Everything works (meaning I do get a connection) apart from this issue.  I'm using credentials to connect in the connect options.  I've been pulling my hair out for 3 days on this issue.  I appreciate any insight.  thanks, Phil

Code:

 

Function RemotePCconnect(ByRef scope As ManagementScope, ByVal remoteMachine As String, ByVal username As String, ByVal password As String, ByVal domain As String, ByRef ErrMsg As String) As Boolean

 

Try

 

    Dim options As ConnectionOptions

    options =

New ConnectionOptions()

    options.Username = username

    options.Password = password

    Dim str_connectionDetails = "\" & remoteMachine & "\root\cimv2"

    scope =

New ManagementScope(str_connectionDetails, options)

    scope.Connect()

 

    Return True

 

Catch ex As Exception

    ErrMsg = ex.Message

 

    Return False

 

End Try

 

End Function

Phil