Connect to a remote computer and displays information about the operating system : Management « Windows « C# / CSharp Tutorial






using System;
using System.Management;
using System.Security;

public class RemoteConnect
{
    public static void Main()
    {
        SecureString pw = new SecureString();
        pw.AppendChar('a');
        pw.MakeReadOnly();

        ConnectionOptions options = new ConnectionOptions("MS_409", "userName", pw,"ntdlmdomain:DOMAIN",
            System.Management.ImpersonationLevel.Impersonate,
            System.Management.AuthenticationLevel.Default, true,
            null, System.TimeSpan.MaxValue);
        pw.Dispose();

        ManagementScope scope =new ManagementScope("\\\\FullComputerName\\root\\cimv2", options);scope.Connect();

        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

        ManagementObjectCollection queryCollection = searcher.Get();
        foreach (ManagementObject m in queryCollection)
        {
            Console.WriteLine("Computer Name : {0}",m["csname"]);
            Console.WriteLine("Windows Directory : {0}",m["WindowsDirectory"]);
            Console.WriteLine("Operating System: {0}",m["Caption"]);
            Console.WriteLine("Version: {0}", m["Version"]);
            Console.WriteLine("Manufacturer : {0}",m["Manufacturer"]);
        }
    }
}








29.18.Management
29.18.1.Get ManagementObjectCollection
29.18.2.An EnumerationOptions variable with an EnumerationOptions constructor and then gets all the instances of a WMI class and its subclasses.
29.18.3.Connect to a remote computer and displays information about the operating system