WMI Remote Control Service

Source: Internet
Author: User
Here we will talk about how to use the System. Management component to operate services on remote and local computers.

As part of the Windows 2000 operating system, WMI provides a scalable and scalable management architecture. the Common Information Model (CIM) is a scalable and object-oriented architecture designed by the Distributed Management Task Standards Association (DMTF, used to manage systems, networks, applications, databases, and devices. Windows Management specifications, also known as CIM for Windows, provide a unified way to access and manage information. For more information about WMI, see MSDN. System. the Management component provides access to a large number of Management information and Management event sets, which are related to Setting Detection Points for systems, devices, and applications according to the Windows Management Specification (WMI) structure.

But the above is not what we are most concerned about. The following are the topics we need to talk about. Undoubtedly, we need to reference the System. Management. Dll assembly and use the classes in the System. Management namespace, such as ManagementClass and ManagementObject. The following uses a class named Win32ServiceManager to encapsulate some operations related to the service. The Code is as follows:

Using System;
Using System. Management;
Namespace ZZ. Wmi
{
Public class Win32ServiceManager
{Private string strPath;
Private ManagementClass managementClass;
Public Win32ServiceManager (): this (".", null, null)
{
}
Public Win32ServiceManager (string host, string userName, string password)
{
This. strPath = "" + host +"
Ootcimv2: Win32_Service ";
This. managementClass = new ManagementClass (strPath );
If (userName! = Null & userName. Length> 0)
{
ConnectionOptions connectionOptions = new ConnectionOptions ();
ConnectionOptions. Username = userName;
ConnectionOptions. Password = password;
ManagementScope managementScope =
New ManagementScope ("" + host +"
Ootcimv2 ", connectionOptions );
This. managementClass. Scope = managementScope;
}
}
// Verify that you can connect to a remote computer
Public static bool RemoteConnectValidate (
String host, string userName, string password)
{
ConnectionOptions connectionOptions = new ConnectionOptions ();
ConnectionOptions. Username = userName;
ConnectionOptions. Password = password;
ManagementScope managementScope = new ManagementScope
("" + Host +"
Ootcimv2 ", connectionOptions );
Try
{
ManagementScope. Connect ();
}
Catch
{
}
Return managementScope. IsConnected;
}
// Obtain the value of the specified service attribute
Public object GetServiceValue (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. managementClass. 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 specified service data of the connected computer
Public string [,] GetServiceList (string [] serverNames)
{
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 service
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 service
Public string PauseService (string serviceName)
{
String strRst = null;
ManagementObject mo = this. managementClass. CreateInstance ();
Mo. Path = new ManagementPath (
This. strPath + ". Name =" "+ serviceName + """);
Try
{
// Determine whether a pause is allowed
If (bool) mo ["acceptPause"] & (string) mo ["State"] = "Running ")
Mo. InvokeMethod ("PauseService", null );
}
Catch (ManagementException e)
{
StrRst = e. Message;
}
Return strRst;
}
// Restore the specified service
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 data can be recovered
If (bool) mo ["acceptPause"] & (string) mo ["State"] = "Paused ")
Mo. InvokeMethod ("ResumeService", null );
}
Catch (ManagementException e)
{
StrRst = e. Message;
}
Return strRst;
}
// Stop the specified service
Public string StopService (string serviceName)
{String strRst = null;
ManagementObject mo = this. managementClass. CreateInstance ();
Mo. Path = new ManagementPath (
This. strPath + ". Name =" "+ serviceName + """);
Try
{
// Determine whether the instance can be stopped
If (bool) mo ["AcceptStop"]) // (string) mo ["State"] = "Running"
Mo. InvokeMethod ("StopService", null );
}
Catch (ManagementException e)
{
StrRst = e. Message;
}
Return strRst;
}
}
}

In Win32ServiceManager, The RemoteConnectValidate static method is used to test whether the connection is successful or not. In addition, the GetServiceValue method, GetServiceList method, and its overload method are provided to obtain service information; the following four methods are used to control the service status.

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.