c#使用wmi需要/ etc / hosts连接到远程

时间:2018-04-27 15:43:40

标签: c# .net wmi

我使用下面的代码连接远程计算机,但是它需要远程计算机必须存在于'@ drivers / etc / hosts'文件中。

如果我没有在/ etc / hosts文件上写下有关远程计算机的信息,则会给出以下错误;

the rpc server is unavailable

如何在不修改'@ drivers / etc / hosts'文件的情况下连接到rdp?

public void GetSystemInformation()
        {

            ConnectionOptions options = new ConnectionOptions();
            options.Username = "test";
            options.Password = "test";
            options.EnablePrivileges = true;

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

            //Query system for Operating System information
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            ManagementObjectCollection queryCollection = searcher.Get();
            foreach (ManagementObject m in queryCollection)
            {
                // Display the remote computer information
                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"]);
            }
        }

0 个答案:

没有答案