Array在scope.Connect();如果我输入它作为ManagementScope scope = new ManagementScope(string.Format(“\\ {0} \ root \ cimv2”,servers,options));但是如果我输入它作为服务器[0]则通过。代码适用于服务器[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();
}
}
答案 0 :(得分:1)
您需要将item
放入其中,而不是整个服务器集合。
foreach (var item in servers)
{
var scope = new ManagementScope(
string.Format(@"\\{0}\root\cimv2", item), options);
scope.Connect();
}