中文资料一大抄,还是英文靠谱啊。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | static void Main(string[] args) { try { #region Code to stop the service //Assign the name of the service you want to stop on the remote machine string serviceName = "audiosrv"; //Assign the user name and password of the account to ConnectionOptions object //which have administrative privilege on the remote machine. ConnectionOptions connectoptions = new ConnectionOptions(); //connectoptions.Impersonation = ImpersonationLevel.Impersonate; connectoptions.Username = @"Domain\UserName"; connectoptions.Password = "Password"; //IP Address of the remote machine string ipAddress = "192.168.206.53"; ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2"); scope.Options = connectoptions; //Define the WMI query to be executed on the remote machine SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'"); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) { ManagementObjectCollection collection=searcher.Get(); foreach (ManagementObject service in collection) { if (service["Started"].Equals(true)) { //Stop the service service.InvokeMethod("StopService", null); } } } Console.ReadLine(); #endregion } catch (Exception ex) { //Log exception in exception log. //Logger.WriteEntry(ex.StackTrace); Console.WriteLine(ex.StackTrace); } } |
via:http://www.go4coding.com/post/2011/02/22/Stop-service-running-on-remote-computer-using-WMI-in-C-Sharp.aspx
确实是这样子的。