I know how to get machines on a server using the System.DirectoryServices network. The problem is that I would like to ignore workstations / computers on the network and only get servers.
In case someone says to check the OS version, the problem with getting the version number of the Windows NT family operating system is that each number can correspond to both server and non-server OS (for example, version NT 6.1, referenced as on Win 7 and Win Server 2008 R2).
Here is my base test class:
namespace Project1
{
class Class1
{
public static void Main(string[] args)
{
List<string> list = Class1.GetComputersOnNetwork();
}
public static List<string> GetComputersOnNetwork()
{
string fileName = "networkcomputers.txt";
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
System.IO.FileStream fs = System.IO.File.Create(fileName, 1024);
StreamWriter strwr = new StreamWriter(fs);
int i = 0;
List<string> list = new List<string>();
DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry computers in root.Children)
{
if ((computers.Name != "Schema"))
{
i++;
Console.WriteLine("Machine Number " + i + ": " + computers.Name);
strwr.WriteLine("Machine Number " + i + ": " + computers.Name);
list.Add(computers.Name);
}
}
return list;
}
}
}
source
share