WMI controls Remote Computer Services
Last Update:2018-12-07
Source: Internet
Author: User
Using system; using system. management; namespace ZZ. WMI {public class win32servicemanager {private string strpath; private managementclass; Public win32servicemanager (): This (". ", null, null) {} public win32servicemanager (string host, string username, string password) {This. strpath = "\\\\" + host + "\\ Root \ cimv2: win32_service"; this. managementclass = new managementclass (strpath); If (usern Ame! = NULL & username. length> 0) {connectionoptions = new connectionoptions (); connectionoptions. username = username; connectionoptions. password = password; managementscope = new managementscope ("\\\\" + host + "\\ Root \ cimv2", connectionoptions); this. managementclass. scope = managementscope; }}// verify whether the machine can be connected to the remote computer public static bool remoteconnectvalidate (string host, string Username, string password) {connectionoptions = new connectionoptions (); connectionoptions. username = username; connectionoptions. password = password; managementscope = new managementscope ("\\\\" + host + "\\ Root \ cimv2", connectionoptions); try {managementscope. connect ();} catch {} return managementscope. isconnected;} // get the value of the specified service attribute public object getserviceval UE (string servicename, string propertyname) {managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); Return Mo [propertyname];} // obtain all service data of the connected computer Public String [,] getservicelist () {string [,] services = new string [this. managementclass. getinstances (). count, 4]; int I = 0; foreach (managementobject Mo in this. managementc Lass. getinstances () {services [I, 0] = (string) Mo ["name"]; services [I, 1] = (string) Mo ["displayname"]; services [I, 2] = (string) Mo ["state"]; services [I, 3] = (string) Mo ["startmode"]; I ++ ;} return services;} // obtain the specified service data of the connected computer Public String [,] getservicelist (string servername) {return getservicelist (New String [] {servername });} // obtain the Public String [,] getservicelist (string [] Serv Ernames) {string [,] services = new string [servernames. length, 4]; managementobject mo = This. managementclass. createinstance (); For (INT I = 0; I <servernames. length; I ++) {Mo. path = new managementpath (this. strpath + ". name = \ "" + servernames [I] + "\" "); services [I, 0] = (string) Mo [" name "]; services [I, 1] = (string) Mo ["displayname"]; services [I, 2] = (string) Mo ["state"]; services [I, 3] = (string) mo ["startmode" ];} Return services;} // stop the specified public string startservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {If (string) Mo [" state "] =" STOPPED ")//! (Bool) Mo ["acceptstop"] Mo. invokemethod ("startservice", null);} catch (managementexception e) {strrst = E. message;} return strrst;} // pause the specified public string pauseservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {// determine whether to suspend if (bool) Mo [" acceptpause "] & (string) mo ["state"] = "running") Mo. invokemethod ("pauseservice", null);} catch (managementexception e) {strrst = E. message;} return strrst;} // restore the specified public string resumeservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {// determine whether the IF (bool) Mo [" acceptpause "] & (string) can be restored) mo ["state"] = "paused") Mo. invokemethod ("resumeservice", null);} catch (managementexception e) {strrst = E. message;} return strrst;} // stop the specified public string stopservice (string servicename) {string strrst = NULL; managementobject mo = This. managementclass. createinstance (); Mo. path = new managementpath (this. strpath + ". name = \ "" + servicename + "\" "); try {// determine whether to stop if (bool) Mo [" acceptstop "]) // (string) mo ["state"] = "running" mo. invokemethod ("stopservice", null);} catch (managementexception e) {strrst = E. message;} return strrst ;}}}