Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access WinRM in 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#?

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

like image 699
Mark Avatar asked Sep 22 '10 17:09

Mark


People also ask

How do I access WinRM?

Log into the Windows console. Optional (For Windows Vista serves as remote server): Start the service "Windows Remote Management " and set it for auto start after reboot. Write the command prompt WinRM quickconfig and press the Enter button.

How do I start WinRM from command line?

Type winrm quickconfig at a command prompt. If you're not running under the local computer Administrator account, then you must either select Run as Administrator from the Start menu, or use the Runas command at a command prompt.

How do I open WinRM ports?

Open WinRM ports in the firewall WinRM uses ports 5985 (HTTP) and 5986 (HTTPS). To open the firewall for port 5985, expand Computer Configuration > Policies > Windows Settings > Security Settings > Windows Firewall with Advanced Security > Windows Firewall with Advanced Security > Inbound Rules.

Where is WinRM?

Where can I use WinRM? You can use WinRM scripting objects, the WinRM command-line tool, or the Windows Remote Shell command line tool WinRS to obtain management data from local and remote computers that may have baseboard management controllers (BMCs).


1 Answers

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

alt text

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

like image 189
serge_gubenko Avatar answered Oct 02 '22 09:10

serge_gubenko