locked
Network user details RRS feed

  • Question

  • Hi

     

     

    ConnectionOptions opt = new ConnectionOptions();
    opt.Username = "userName";
    opt.Password = "password";
    opt.Authentication = AuthenticationLevel.Default;
    opt.Impersonation = ImpersonationLevel.Impersonate;


    ManagementScope msc = new ManagementScope("\\\\IP Address" + "\\root\\cimv2", opt);

    string queryString = "select LogonId from win32_logonsession where logontype= 2";
    ManagementObjectSearcher query = new ManagementObjectSearcher(msc, new SelectQuery(queryString));

    ManagementObjectCollection oManQueryCollection1 = query.Get();

     

     

    On execution of the last line of code. It crashes. Following is the detail.

     

    System.Runtime.InteropServices.COMException was caught
      Message="The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"
      Source="System.Management"
      ErrorCode=-2147023174
      StackTrace:
           at System.Management.ThreadDispatch.Start()
           at System.Management.ManagementScope.Initialize()
           at System.Management.ManagementObjectSearcher.Initialize()
           at System.Management.ManagementObjectSearcher.Get()
           at WindowsFormsApplication1.User.test() in D:\abc\def\User.cs:line 46
      InnerException:

     

    Please suggest something.




    Tuesday, April 12, 2011 8:44 AM

Answers

  • If you wish, execute commands that below on client, and then you try again:
    netsh.exe firewall set service type=REMOTEADMIN mode=ENABLE scope=ALL
    netsh firewall add portopening protocol=tcp port=135 name=DCOM_TCP135 
    • Proposed as answer by Larcolais Gong Monday, April 18, 2011 12:21 PM
    • Unproposed as answer by Larcolais Gong Monday, April 18, 2011 12:21 PM
    • Marked as answer by Larcolais Gong Wednesday, April 20, 2011 2:49 PM
    Tuesday, April 12, 2011 9:29 AM
  • I just wrote the same instance myself with the exception of the impersonation. My version succeeded flawlessly, so I would say that it may actually have something to do with the RPC service on the box.. I would attempt to try another box on the network. My code is below if you wanted to try it.. Just replace COMPUTER with the corresponding IP Address.

    ManagementScope scope = new ManagementScope(@"\\COMPUTER\root\cimv2");
    scope.Connect();
    
    ObjectQuery query = new ObjectQuery("select LogonId from Win32_LogonSession where LogonType = 2");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection objColl = searcher.Get();
    
    foreach (ManagementObject o in objColl)
    {
      Console.WriteLine("Test: " + o);
    }
    
    • Marked as answer by Larcolais Gong Wednesday, April 20, 2011 2:49 PM
    Thursday, April 14, 2011 6:13 PM

All replies

  • Your IP address that specified to below is valid?

    ManagementScope msc = new ManagementScope("\\\\IP Address" + "\\root\\cimv2", opt);
    Tuesday, April 12, 2011 9:09 AM
  • Yes. It is valid. I created a mstsc program to open the remote computer. It's fine there.
    Tuesday, April 12, 2011 9:14 AM
  • If you wish, execute commands that below on client, and then you try again:
    netsh.exe firewall set service type=REMOTEADMIN mode=ENABLE scope=ALL
    netsh firewall add portopening protocol=tcp port=135 name=DCOM_TCP135 
    • Proposed as answer by Larcolais Gong Monday, April 18, 2011 12:21 PM
    • Unproposed as answer by Larcolais Gong Monday, April 18, 2011 12:21 PM
    • Marked as answer by Larcolais Gong Wednesday, April 20, 2011 2:49 PM
    Tuesday, April 12, 2011 9:29 AM
  • I just wrote the same instance myself with the exception of the impersonation. My version succeeded flawlessly, so I would say that it may actually have something to do with the RPC service on the box.. I would attempt to try another box on the network. My code is below if you wanted to try it.. Just replace COMPUTER with the corresponding IP Address.

    ManagementScope scope = new ManagementScope(@"\\COMPUTER\root\cimv2");
    scope.Connect();
    
    ObjectQuery query = new ObjectQuery("select LogonId from Win32_LogonSession where LogonType = 2");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection objColl = searcher.Get();
    
    foreach (ManagementObject o in objColl)
    {
      Console.WriteLine("Test: " + o);
    }
    
    • Marked as answer by Larcolais Gong Wednesday, April 20, 2011 2:49 PM
    Thursday, April 14, 2011 6:13 PM
  • Any update? Has your question been solved?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 18, 2011 12:21 PM