How to Connect the remote PC Using C# For Start/Stop the Windows Services

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnalla
    New Member
    • Nov 2008
    • 13

    How to Connect the remote PC Using C# For Start/Stop the Windows Services

    Hi,
    I am Connecting ti remote computer using C# WMI through the following code.But i t gives Some error "INVALID PARAMETERS" at scop.Connet
    Is there any procedure to coonect to rempte pc with authentication and start and stop the remote pc Windows services.

    plse suggest me any modification in the code and tell mw the other code . Its Urgent.

    Code:
    try 
                { 
                    ConnectionOptions connection = new ConnectionOptions(); 
                    connection.Username = userNameBox.Text; 
                    connection.Password = passwordBox.Text; 
                    connection.Authority = "ntlmdomain:DOMAIN"; 
      
                    ManagementScope scope = new ManagementScope( 
                        "\\\\FullComputerName\\root\\CIMV2", connection); 
                    scope.Connect(); 
      
                    ObjectQuery query= new ObjectQuery( 
                        "SELECT * FROM Win32_Service");  
      
                    ManagementObjectSearcher searcher =  
                        new ManagementObjectSearcher(scope, query); 
      
                    foreach (ManagementObject queryObj in searcher.Get()) 
                    { 
                        Console.WriteLine("-----------------------------------"); 
                        Console.WriteLine("Win32_Service instance"); 
                        Console.WriteLine("-----------------------------------"); 
                        Console.WriteLine("Caption: {0}", queryObj["Caption"]); 
                        Console.WriteLine("Description: {0}", queryObj["Description"]); 
                        Console.WriteLine("Name: {0}", queryObj["Name"]); 
                        Console.WriteLine("PathName: {0}", queryObj["PathName"]); 
                        Console.WriteLine("State: {0}", queryObj["State"]); 
                        Console.WriteLine("Status: {0}", queryObj["Status"]); 
                    } 
                    Close(); 
                } 
                catch(ManagementException err) 
                { 
                    MessageBox.Show("An error occured while querying for WMI data: " 
                        + err.Message); 
                }
    Thanks
    Prasad.
  • Ramk
    New Member
    • Nov 2008
    • 61

    #2
    Before going to the problem, I want to remind you that, do wrap your code snippets around the [code] tags.

    To use WMI, windows firewall should be disabled on the remote pc.

    I got this output(omitted the entire output)...
    Code:
    -----------------------------------\nWin32_Service instance-----------------------------------Caption: Alerter
    Description: Notifies selected users and computers
    of administrative alerts. If the service is stopped, programs that use administrative alerts will not receive them. If this service is disabled,
    any services that explicitly depend on it will fail to start.
    Name: Alerter
    PathName: C:\\WINDOWS\\system32\\svchost.exe -k LocalService
    State: Running
    Status: OK
    With firewall enabled, you may receive the following message
    The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    Last edited by Ramk; Dec 11 '08, 11:27 AM. Reason: Added the exception message...

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      Originally posted by pnalla
      Hi,
      I am Connecting ti remote computer using C# WMI through the following code.But i t gives Some error "INVALID PARAMETERS" at scop.Connet
      Is there any procedure to coonect to rempte pc with authentication and start and stop the remote pc Windows services.

      plse suggest me any modification in the code and tell mw the other code . Its Urgent.

      try
      {
      ConnectionOptio ns connection = new ConnectionOptio ns();
      connection.User name = userNameBox.Tex t;
      connection.Pass word = passwordBox.Tex t;
      connection.Auth ority = "ntlmdomain:DOM AIN";

      ManagementScope scope = new ManagementScope (
      "\\\\FullComput erName\\root\\C IMV2", connection);
      scope.Connect() ;

      ObjectQuery query= new ObjectQuery(
      "SELECT * FROM Win32_Service") ;

      ManagementObjec tSearcher searcher =
      new ManagementObjec tSearcher(scope , query);

      foreach (ManagementObje ct queryObj in searcher.Get())
      {
      Console.WriteLi ne("-----------------------------------");
      Console.WriteLi ne("Win32_Servi ce instance");
      Console.WriteLi ne("-----------------------------------");
      Console.WriteLi ne("Caption: {0}", queryObj["Caption"]);
      Console.WriteLi ne("Description : {0}", queryObj["Descriptio n"]);
      Console.WriteLi ne("Name: {0}", queryObj["Name"]);
      Console.WriteLi ne("PathName: {0}", queryObj["PathName"]);
      Console.WriteLi ne("State: {0}", queryObj["State"]);
      Console.WriteLi ne("Status: {0}", queryObj["Status"]);
      }
      Close();
      }
      catch(Managemen tException err)
      {
      MessageBox.Show ("An error occured while querying for WMI data: "
      + err.Message);
      }

      Thanks
      Prasad.
      Inaddition to WMI you can also look at ServiceControll er Class. You need to specify Machine name and service name for remote pcs..
      For WMI to work on remote pcs.. specially for admin tasks, there are certain issues... do check the links posted here
      WMI Remote
      Also check this implementation on MSDN start stop services

      Comment

      • zaurb
        New Member
        • Feb 2010
        • 1

        #4
        did you try adding:
        options.Imperso nation = ImpersonationLe vel.Impersonate ;
        just after:
        ConnectionOptio ns connection = new ConnectionOptio ns();

        ?

        Comment

        Working...