我想创建一个可以使用WINRM而不是WMI收集系统信息(WIN32_BLABLABLA)的小型应用程序。我该如何从C#做到这一点?

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

有帮助吗?

解决方案

我想最简单的方法是使用WSMAN自动化。您的项目中的Windwos System32参考wsmauto.dll:

alt text

然后,下面的代码应为您工作。 API描述在这里: 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);
    }
}

希望这会有所帮助

其他提示

我有一篇文章描述了一种简单的方法,可以通过.net从.net上运行PowerShell http://getthinktank.com/2015/06/22/naos-winrm-windows-remote-management-management-through-net/.

如果您只想复制它,则该代码位于单个文件中,并且它也是一个nuget软件包,其中包括对system.management.automation的引用。

IT自动管理受信任的主机,可以运行脚本块并发送文件(该文件并没有真正支持,但我创建了一个工作)。回报始终是PowerShell的原始对象。

// this is the entrypoint to interact with the system (interfaced for testing).
var machineManager = new MachineManager(
    "10.0.0.1",
    "Administrator",
    MachineManager.ConvertStringToSecureString("xxx"),
    true);

// will perform a user initiated reboot.
machineManager.Reboot();

// can run random script blocks WITH parameters.
var fileObjects = machineManager.RunScript(
    "{ param($path) ls $path }",
    new[] { @"C:\PathToList" });

// can transfer files to the remote server (over WinRM's protocol!).
var localFilePath = @"D:\Temp\BigFileLocal.nupkg";
var fileBytes = File.ReadAllBytes(localFilePath);
var remoteFilePath = @"D:\Temp\BigFileRemote.nupkg";
machineManager.SendFile(remoteFilePath, fileBytes);

希望这会有所帮助,我已经在自动部署中使用了一段时间。如果您发现问题,请发表评论。

我想注意,这显示了Visual Studio 2010中默认情况下的Interop错误。
CF http://blogs.msdn.com/b/mshneer/archive/2009/12/07/interop-type-xxx-cannot-be-be-embedded-use-use-papplicable-inplicable-interface-instead.aspx

似乎有两种解决这个问题的方法。首先记录在上面列出的文章中,似乎是解决问题的正确方法。此示例的相关更改是:

wsman wsmanobject = new wsman();这代替了IWSMAN WSMAN = New Wsmanclass();这将丢弃错误。

第二个分辨率是转到VS2010 - >解决方案资源管理器 - >解决方案 - > project->参考,并选择WSManauutomation。右键单击或点击Alt-Enter以访问属性。更改WSMAUTO参考的“嵌入Interop类型”属性的值。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top