I know how to get the machines on a server using the System.DirectoryServices network.
The issue is that I would like to ignore workstations/computers on the network and only retrieve servers.
In case someone says to check the OS version, the problem with getting a Win NT family OS version number is that each number may correspond to both a server and non-server OS (such as NT version 6.1 referring both to Win 7 and Win Server 2008 R2).
Here is my basic 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";
// Delete the file if it exists.
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
// Create the file.
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;
}
}
}
Instead of going at the
operatingSystemVersionproperty, look at theoperatingSystemproperty. That’ll give you the name of the SKU. You’ll need to know which are server OS versions and which aren’t – there’s noIsServerboolean. Depending on how they’re named, you may be able to do a wildcard search onoperatingSystemVersionto find computers that have aoperatingSystemVersionthat contains the string “server”.