数组在作用域失败。连接();如果我将其输入为管理范围范围 = 新的管理范围(字符串。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();
}
}
您需要
将item
放入其中,而不是整个服务器集合。
foreach (var item in servers)
{
var scope = new ManagementScope(
string.Format(@"\{0}rootcimv2", item), options);
scope.Connect();
}