WMI controls Remote Computer Services

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 ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.