469,592 Members | 1,927 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

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

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
3 26338
Ramk
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
PRR
750 Expert 512MB
@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
zaurb
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.

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
By using this site, you agree to our Privacy Policy and Terms of Use.