WS-Man (windows service management) and c#
来源:互联网 发布:晋中市教研网络平台 编辑:程序博客网 时间:2024/05/20 12:46
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
BatchItems
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.
Error
Read-only
Gets additional error information in an XML stream.
Timeout
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. :)
- WS-Man (windows service management) and c#
- Preparing for Application and Service Deployment-Application Life Cycle Management for Windows Azure
- Windows Rights Management Service RMS部署实战
- How to create (and deploy) a windows service in C# ?
- Microsoft Windows Vista Management and Administration
- C# Dispose, Finalization, and Resource Management
- C#创建Windows Service
- C# Windows Service 添加
- c#创建Windows Service
- C#创建Windows Service
- 从WS-DM,WS-Man到WS-Resource Catalog
- linux base note(2)(Vim,man,soft-package-management,xinetd and some others)
- windows service and target
- JAX-WS Web Service
- Bad man and Bad man
- 用C#创建Windows Service
- 用C#创建Windows Service
- 用C#创建Windows Service
- 特征值与特征向量概念
- linux内核编译全过程
- Javascript实现的软键盘,效果不错
- Sql Server中NULLIF和ISNULL的说明
- SDL Linux下的使用 计算机图形学
- WS-Man (windows service management) and c#
- 如何修改Linux主机名
- 开源项目live555学习心得(三)
- 类 java.math.BigInteger
- PHP易混淆函数的区分方法及意义
- C#命名规范
- 类 java.math.BigInteger
- bind() 10048错误的解决方法
- php中单引号和双引号的区别