By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

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

P: 13
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.

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"; 
  7.  
  8.                 ManagementScope scope = new ManagementScope( 
  9.                     "\\\\FullComputerName\\root\\CIMV2", connection); 
  10.                 scope.Connect(); 
  11.  
  12.                 ObjectQuery query= new ObjectQuery( 
  13.                     "SELECT * FROM Win32_Service");  
  14.  
  15.                 ManagementObjectSearcher searcher =  
  16.                     new ManagementObjectSearcher(scope, query); 
  17.  
  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.             } 
  37.  
Thanks
Prasad.
Dec 11 '08 #1
Share this Question
Share on Google+
3 Replies


P: 61
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

Expert 100+
P: 750
PRR
@pnalla
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

P: 1
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.