数组在 scope.Connect() 处失败;如果输入为 ManagementScope scope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", servers, options));

Array fails at scope.Connect(); If entered as ManagementScope scope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", servers, options));

数组在 scope.Connect() 处失败;如果我将其输入为 ManagementScope scope = new ManagementScope(string.Format("\\{0}\root\cimv2", servers, options)); 但如果我将其输入为服务器 [0],则通过。代码在 servers[0] 上运行良好,但我需要遍历数组。有任何想法吗?提前致谢。

protected void ServerServices()
    {
        string txt = serverName.Text;
        string[] servers= txt.Split(new Char[] { '\n', '\r' }, 
        StringSplitOptions.RemoveEmptyEntries);

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

        foreach (string item in servers)
        {
            //Create the scope that will enter code here connect to the 
             default `enter code here`root for WMI          
            ManagementScope scope = new ManagementScope(string.Format("\\
    `       enter code here`{0}\root\cimv2", servers[0], options));

            scope.Connect();
        }
}

您需要将 item 放入其中,而不是整个服务器集合。

foreach (var item in servers)
{      
    var scope = new ManagementScope(
       string.Format(@"\{0}\root\cimv2", item), options);
    scope.Connect();
}