Search code examples
c#asp.netarrayswmi

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


The Array fails at scope.Connect(); if I have it entered as ManagementScope scope = new ManagementScope(string.Format("\\{0}\root\cimv2", servers, options));but passes if I have it entered as servers[0]. Code works fine with servers[0] but I need to loop through the array. Any ideas? Thanks in advance.

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();
        }
}

Solution

  • You need to put item into it, not the whole collection of servers.

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