471,898 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,898 software developers and data experts.

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

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.

Expand|Select|Wrap|Line Numbers
  1. try 
  2.             { 
  3.                 ConnectionOptions connection = new ConnectionOptions(); 
  4.                 connection.Username = userNameBox.Text; 
  5.                 connection.Password = passwordBox.Text; 
  6.                 connection.Authority = "ntlmdomain:DOMAIN"; 
  8.                 ManagementScope scope = new ManagementScope( 
  9.                     "\\\\FullComputerName\\root\\CIMV2", connection); 
  10.                 scope.Connect(); 
  12.                 ObjectQuery query= new ObjectQuery( 
  13.                     "SELECT * FROM Win32_Service");  
  15.                 ManagementObjectSearcher searcher =  
  16.                     new ManagementObjectSearcher(scope, query); 
  18.                 foreach (ManagementObject queryObj in searcher.Get()) 
  19.                 { 
  20.                     Console.WriteLine("-----------------------------------"); 
  21.                     Console.WriteLine("Win32_Service instance"); 
  22.                     Console.WriteLine("-----------------------------------"); 
  23.                     Console.WriteLine("Caption: {0}", queryObj["Caption"]); 
  24.                     Console.WriteLine("Description: {0}", queryObj["Description"]); 
  25.                     Console.WriteLine("Name: {0}", queryObj["Name"]); 
  26.                     Console.WriteLine("PathName: {0}", queryObj["PathName"]); 
  27.                     Console.WriteLine("State: {0}", queryObj["State"]); 
  28.                     Console.WriteLine("Status: {0}", queryObj["Status"]); 
  29.                 } 
  30.                 Close(); 
  31.             } 
  32.             catch(ManagementException err) 
  33.             { 
  34.                 MessageBox.Show("An error occured while querying for WMI data: " 
  35.                     + err.Message); 
  36.             } 
Dec 11 '08 #1
3 27114
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)...
Expand|Select|Wrap|Line Numbers
  1. -----------------------------------\nWin32_Service instance-----------------------------------Caption: Alerter
  2. Description: Notifies selected users and computers
  3. of administrative alerts. If the service is stopped, programs that use administrative alerts will not receive them. If this service is disabled,
  4. any services that explicitly depend on it will fail to start.
  5. Name: Alerter
  6. PathName: C:\\WINDOWS\\system32\\svchost.exe -k LocalService
  7. State: Running
  8. Status: OK
With firewall enabled, you may receive the following message
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
Dec 11 '08 #2
750 Expert 512MB
Inaddition to WMI you can also look at ServiceController 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
Dec 11 '08 #3
did you try adding:
options.Impersonation = ImpersonationLevel.Impersonate;
just after:
ConnectionOptions connection = new ConnectionOptions();

Feb 4 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by LP | last post: by
3 posts views Thread by Fabian Knopf | last post: by
1 post views Thread by Antonello Calabṛ | last post: by
3 posts views Thread by Goran Djuranovic | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.