I have investigated Windows Service Management several days. And finally I found a way to implement in C#.
First, i'd like to introduce windows remote management.
Windows Remote Management(WinRm) is the microsoft implementation of WS-Management Protocal, SOAP-Based, firewall-friendly protocol that allows hardware and operating systems to interoperate.
You can obtain hardware and system data from WS-Management protocol implementations running on operating systems. WinRM establishes a session with a remote computer through the SOAP-based WS-Management protocol. Data returned to WS-Management protocol are formatted in XML.
The following are steps and examples:
1. Create a windows console project. Add WsmAuto.dll (browser it from system disk) to the project. Open Reference node under your project, it has added WSManAutomation item. Right click WSManAutomation item and choose Properties, change “Embed Interop Types” property to False (check this blog for the reason).
2. Create a WSManClass:
WSManClass wsman = new WSManClass();
3.Session
//Create an instance of the WsManClass Object
WSManClass wsmanClass = new WSManClass();
//Create a session on the current computer
IWSManSession session = (IWSManSession)wsmanClass.CreateSession(null, 0, null);
//Create a session on a remote computer
//IWSManSession session = (IWSManSession)wsmanClass.CreateSession("ConnectionString", 0, null);
//Get
//return a xml string with win32_currenttime information
String strResult = session.Get("wmi/root/cimv2/win32_currenttime");
//Put
//update resource content
//return a xml string of all Win32_WMISetting information with modified data
string strResourceUri = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_WMISetting";
//It only changes the p:LoggingLevel value
string newXmlContent = "<p:Win32_WMISetting xmlns:p=/"http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_WMISetting/"><p:LoggingLevel>0</p:LoggingLevel></p:Win32_WMISetting>";
string strResult = session.Put(strResourceUri, newXmlContent, 0);
//Invoke
//Invokes a method and returns the results of the method call.
string strResource = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process";
string strInputParameters = "<p:Create_INPUT xmlns:p=/"http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process/"><p:CommandLine>calc.exe</p:CommandLine></p:Create_INPUT>";
// start a Caculator application
string strResult = session.Invoke("Create", strResource, strInputParameters, 0);
Console.WriteLine(strResult);
// output
// <p:Create_OUTPUT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="
//http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process" xmlns:ci
//m="http://schemas.dmtf.org/wbem/wscim/1/common" xml:lang="en-US">
//<p:ProcessId>4360</p:ProcessId>
//<p:ReturnValue>0</p:ReturnValue>
//</p:Create_OUTPUT>
Identify
The Session.Identify method queries a remote computer to determine if it supports the WS-Management protocol.
The following VBScript example sends an unauthenticated Identify request to the remote computer named Remote in the same domain.
set WSMan = CreateObject("Wsman.Automation")
set Session = WSMan.CreateSession("Remote", _
WSMan.SessionFlagUseNoAuthentication)
WScript.Echo Session.Identify
Enumerate
return IWSManEnumerate object
string strResource = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_LogicalDisk";
IWSManEnumerator objResult = (IWSManEnumerator)session.Enumerate(strResource);
use filter
//string strResource01 = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*";
//string strDialect = "http://schemas.microsoft.com/wbem/wsman/1/WQL";
//string strFilter = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IpEnabled=TRUE";
//IWSManEnumerator objResult01 = (IWSManEnumerator)session.Enumerate(strResource01,strFilter,strDialect,0);
//while (!objResult.AtEndOfStream)
//{
// Console.WriteLine(objResult.ReadItem());
//}
//delete
string strResource = "http://schemas.microsoft.com/wbem/wsman/1/config/Listener?Address=*+Transport=HTTP";
session.Delete(strResource);
Console.WriteLine(session.Get(strResource, 0));
//create
string resourceUri = "http://schemas.microsoft.com/wbem/wsman/1/config/Listener?Address=*+Transport=HTTP";
string inputXml = "<cfg:Listener xmlns:cfg=/"http://schemas.dmtf.org/wbem/wsman/1/config/Listener.xsd/"><cfg:Hostname>GetFQDNName()</cfg:Hostname></cfg:Listener>";
string strResult = session.Create(resourceUri, inputXml, 0);
Console.WriteLine(strResult);
Property | Access type | Description |
Read/write | Sets and gets the number of items in each enumeration batch. This value cannot be changed during an enumeration. By default, the default is an unlimited number of items. The resource provider may set a limit. | |
Read-only | Gets additional error information in an XML stream. | |
Read/write | Sets and gets the maximum amount of time (in milliseconds) for the client application to wait. |
The c# code is based on VBScript code in the MSDN document. If you have anything unclear, please refer to:
http://msdn.microsoft.com/en-us/library/aa384469.aspx
Hope you have a nice day. :)