本文介绍了如何访问WinRM的在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用WinRM的,而不是WMI的小应用程序,可以收集系统信息(Win32_blablabla)。我怎样才能做到这一点从C#?

I'd like to create a small application that can collect system information (Win32_blablabla) using WinRM as opposed to WMI. How can i do that from C#?

主要目标是使用,而不是DCOM(WMI)WS-MAN(WinRM的)。

The main goal is to use WS-Man (WinRm) as opposed to DCOM (WMI).

推荐答案

我想最简单的方法是使用自动化WSMAN。

I guess the easiest way would be to use WSMAN automation. Reference wsmauto.dll from windwos\system32 in your project:

那么,下面的代码应该为你工作。 API说明是在这里:

then, code below should work for you. API description is here: msdn: WinRM C++ API

IWSMan wsman = new WSManClass();
IWSManConnectionOptions options = (IWSManConnectionOptions)wsman.CreateConnectionOptions();                
if (options != null)
{
    try
    {
        // options.UserName = ???;  
        // options.Password = ???;  
        IWSManSession session = (IWSManSession)wsman.CreateSession("http://<your_server_name>/wsman", 0, options);
        if (session != null)
        {
            try
            {
                // retrieve the Win32_Service xml representation
                var reply = session.Get("http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=winmgmt", 0);
                // parse xml and dump service name and description
                var doc = new XmlDocument();
                doc.LoadXml(reply);
                foreach (var elementName in new string[] { "p:Caption", "p:Description" })
                {
                    var node = doc.GetElementsByTagName(elementName)[0];
                    if (node != null) Console.WriteLine(node.InnerText);
                }
            }
            finally
            {
                Marshal.ReleaseComObject(session);
            }
        }
    }
    finally
    {
        Marshal.ReleaseComObject(options);
    }
}



希望这可以帮助,至于

hope this helps, regards

这篇关于如何访问WinRM的在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:49