【问题标题】:How to access WinRM in C#如何在 C# 中访问 WinRM
【发布时间】:2010-09-22 17:24:24
【问题描述】:

我想创建一个小型应用程序,它可以使用 WinRM 而不是 WMI 来收集系统信息 (Win32_blablabla)。我如何从 C# 中做到这一点?

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

【问题讨论】:

    标签: c# windows wmi remote-management


    【解决方案1】:

    我想最简单的方法是使用 WSMAN 自动化。在您的项目中从 windwos\system32 引用 wsmauto.dll:

    那么,下面的代码应该适合您。 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);
        }
    }
    

    希望这会有所帮助,问候

    【讨论】:

      【解决方案2】:

      我在http://getthinktank.com/2015/06/22/naos-winrm-windows-remote-management-through-net/ 有一篇文章,介绍了一种通过 WinRM 从 .NET 运行 Powershell 的简单方法。

      如果您只想复制代码,该代码位于单个文件中,它也是一个 NuGet 包,其中包含对 System.Management.Automation 的引用。

      它自动管理受信任的主机,可以运行脚本块,还可以发送文件(这不是真正支持的,但我创建了一个解决方法)。返回的总是来自 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);
      

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

      【讨论】:

        【解决方案3】:

        我想指出,这在 Visual Studio 2010 中默认显示互操作错误。
        参考文献http://blogs.msdn.com/b/mshneer/archive/2009/12/07/interop-type-xxx-cannot-be-embedded-use-the-applicable-interface-instead.aspx

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

        WSMan wsManObject = new WSMan(); 这代替了 IWSMan wsman = new WSManClass();这将引发错误。

        第二个解决方法是去VS2010—>Solution Explorer—>Solution—>Project—>References,选择WSManAutomation。右键单击或按 Alt-Enter 以访问属性。更改 wsmauto 引用的“嵌入互操作类型”属性的值。

        【讨论】:

          猜你喜欢
          • 2021-03-01
          • 1970-01-01
          • 2021-05-31
          • 1970-01-01
          • 2018-04-25
          • 2012-06-13
          • 2010-09-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多