469,568 Members | 1,464 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

List Computers on my domain

JB
I am trying to get a list of all the active computers running on my
domain.
I'm writing some remote management style software with WMI, which
works fine when i know the computer name, but i just want to be able
to produce a list and work it from there.

I found the following method:

String path = "WinNT://MY.DOMAIN.COM";
String username = "administrator";
String password = "password";

DirectoryEntry domain = new DirectoryEntry(path, username, password);
DirectoryEntries computers = domain.Children;
computers.SchemaFilter.Add("computer");
foreach(DirectoryEntry Computer in computers)
{
String a = Computer.Name;
}

which i found on this group somewhere, but i keep getting the
following error:
Multiple connections to a server or shared resource by the same user,
using more than one user name, are not allowed.
This happens whether i use administrator or my own username (or even
if i am logged in as administrator).

-
I was able to use a similar method:

DirectoryEntry objDE = new DirectoryEntry("LDAP://my.domain.com");
DirectorySearcher objSearcher = new DirectorySearcher(objDE);
objSearcher.Filter = ("(ObjectClass=computer)");

Which works.. but this returns computers that haven't been switched on
in a long long time.
In fact it generates almost 3 times as many results as a NET VIEW
command.
I could ping each name and see if it responds, but this will add a lot
onto the execution time, which i'm trying to avoid.
(Plus it has CN= at the beginning of every name, which is a little
annoying.)

So, can anyone inform me how to solve the first problem? Or how to
filter for only active/switched on computers in the second method?
Or would i be best going down another route?
I was thinking of just running a NET VIEW and catching the output,
filtering it and voila! But that seems like a really obfuscated way of
going about it.
Cheers

May 22 '07 #1
4 7373
On 2007-05-22 17:42:57 +0100, JB <ja*******@gmail.comsaid:
I am trying to get a list of all the active computers running on my
domain.
I'm writing some remote management style software with WMI, which
works fine when i know the computer name, but i just want to be able
to produce a list and work it from there.

I found the following method:

String path = "WinNT://MY.DOMAIN.COM";
String username = "administrator";
String password = "password";

DirectoryEntry domain = new DirectoryEntry(path, username, password);
DirectoryEntries computers = domain.Children;
computers.SchemaFilter.Add("computer");
foreach(DirectoryEntry Computer in computers)
{
String a = Computer.Name;
}

which i found on this group somewhere, but i keep getting the
following error:
Multiple connections to a server or shared resource by the same user,
using more than one user name, are not allowed.
This happens whether i use administrator or my own username (or even
if i am logged in as administrator).

-
I was able to use a similar method:

DirectoryEntry objDE = new DirectoryEntry("LDAP://my.domain.com");
DirectorySearcher objSearcher = new DirectorySearcher(objDE);
objSearcher.Filter = ("(ObjectClass=computer)");

Which works.. but this returns computers that haven't been switched on
in a long long time.
In fact it generates almost 3 times as many results as a NET VIEW
command.
I could ping each name and see if it responds, but this will add a lot
onto the execution time, which i'm trying to avoid.
(Plus it has CN= at the beginning of every name, which is a little
annoying.)

So, can anyone inform me how to solve the first problem? Or how to
filter for only active/switched on computers in the second method?
Or would i be best going down another route?
I was thinking of just running a NET VIEW and catching the output,
filtering it and voila! But that seems like a really obfuscated way of
going about it.
Cheers
The way I tackled a similar problem was to first omit any computer
objects that are disabled then calculate the likelyhood of a computer
object being "active" based on the PwdLastSet attribute. This will
narrow your search somewhat but you'll stil run into computers that are
not switched on at that moment in time. Actually, I am planning on
implementing a similar feature such that the results of the
DirectorySearcher can be fed into another interactive process and my
plan is to use a WMI ping to determine if the computer can be accessed
remotely.

narrowing the search to include only non-disabled objects:

* searcher.filter =
"(&(ObjectCategory=computer)(!UserAccountControl:1 .2.840.113556.1.4.803:=2))";
speed

up the search by only selecting the attributes you are interested in:

* searcher.propertiesToLoad.Add("cn");
* searcher.propertiesToLoad.Add("PwdLastSet");

I should say at this point I have to calculate the PwdLastSet time
because we are not running a windows 2003 domain in native mode. I
believe in 2k3 the LastLogon time stamp (or something similar) is
replicated to all DCs which will make this method redundant. If you
have a 2k3 domain I would investigate looking for that attribute.
However . . .

.. . .Now calculate a suitable cut off period based on the PwdLastSet attribute
[your routine for itterating through the list of comp objects in the
searchresults]

* DateTime today = DateTime.Now;
* DateTime cutoff = today.Subtract(new TimeSpan(30,0,0,0)); //30 Days
* //I'm not sure if the following line is exactly necessary but I use
it as I need to output a friendly time
* DateTime compPwdLastSet =
DateTime.FromFileTime((long)comp.Properties["PwdLastSet"][0]);

* If (compPwdLastSet <= cutoff)
* {
* //object is probably stale
* }
I'd be interested to see what you do with the machines once you have a list :-)

Cheers,
n

May 23 '07 #2
JB
>
The way I tackled a similar problem was to first omit any computer
objects that are disabled then calculate the likelyhood of a computer
object being "active" based on the PwdLastSet attribute. This will
narrow your search somewhat but you'll stil run into computers that are
not switched on at that moment in time. Actually, I am planning on
implementing a similar feature such that the results of the
DirectorySearcher can be fed into another interactive process and my
plan is to use a WMI ping to determine if the computer can be accessed
remotely.

narrowing the search to include only non-disabled objects:

* searcher.filter =
"(&(ObjectCategory=computer)(!UserAccountControl:1 .2.840.113556.1.4.803:=2))";

speed

up the search by only selecting the attributes you are interested in:

* searcher.propertiesToLoad.Add("cn");
* searcher.propertiesToLoad.Add("PwdLastSet");

I should say at this point I have to calculate the PwdLastSet time
because we are not running a windows 2003 domain in native mode. I
believe in 2k3 the LastLogon time stamp (or something similar) is
replicated to all DCs which will make this method redundant. If you
have a 2k3 domain I would investigate looking for that attribute.
However . . .

. . .Now calculate a suitable cut off period based on the PwdLastSet attribute

[your routine for itterating through the list of comp objects in the
searchresults]

* DateTime today = DateTime.Now;
* DateTime cutoff = today.Subtract(new TimeSpan(30,0,0,0)); //30 Days
* //I'm not sure if the following line is exactly necessary but I use
it as I need to output a friendly time
* DateTime compPwdLastSet =
DateTime.FromFileTime((long)comp.Properties["PwdLastSet"][0]);

* If (compPwdLastSet <= cutoff)
* {
* //object is probably stale
* }

I'd be interested to see what you do with the machines once you have a list :-)

Cheers,
n

Thanks, that has helped me, but now I have another, very strange
error..
The results it returns are still pretty far wide of the mark.
I used the "LastLogon" Property and filtered out any machines over 30
days ago.
Now its still showing machines that havent been used in months/years.
And some anomalies are cropping up.

It shows the following computer names for example:
10-07
10-07-Vista

Neither of these are now valid machines, however,
When i ping these from a command prompt, they show the same IP
address.
when i ping that IP address with the -a switch to resolve the machine
name, It returns a third, completely different name.
Some PC's are ok, others are not.

This is a network/active directory problem i'm sure. But if anyone
knows why it would do this I would appreciate it.

No simple way to simply reproduce the list from Net View or My Network
Places? Those show the exact results i'm looking for..
May 23 '07 #3
"JB" <ja*******@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...

The way I tackled a similar problem was to first omit any computer
objects that are disabled then calculate the likelyhood of a computer
object being "active" based on the PwdLastSet attribute. This will
narrow your search somewhat but you'll stil run into computers that are
not switched on at that moment in time. Actually, I am planning on
implementing a similar feature such that the results of the
DirectorySearcher can be fed into another interactive process and my
plan is to use a WMI ping to determine if the computer can be accessed
remotely.

narrowing the search to include only non-disabled objects:

* searcher.filter =
"(&(ObjectCategory=computer)(!UserAccountControl: 1.2.840.113556.1.4.803:=2))";

speed

up the search by only selecting the attributes you are interested in:

* searcher.propertiesToLoad.Add("cn");
* searcher.propertiesToLoad.Add("PwdLastSet");

I should say at this point I have to calculate the PwdLastSet time
because we are not running a windows 2003 domain in native mode. I
believe in 2k3 the LastLogon time stamp (or something similar) is
replicated to all DCs which will make this method redundant. If you
have a 2k3 domain I would investigate looking for that attribute.
However . . .

. . .Now calculate a suitable cut off period based on the PwdLastSet
attribute

[your routine for itterating through the list of comp objects in the
searchresults]

* DateTime today = DateTime.Now;
* DateTime cutoff = today.Subtract(new TimeSpan(30,0,0,0)); //30 Days
* //I'm not sure if the following line is exactly necessary but I use
it as I need to output a friendly time
* DateTime compPwdLastSet =
DateTime.FromFileTime((long)comp.Properties["PwdLastSet"][0]);

* If (compPwdLastSet <= cutoff)
* {
* //object is probably stale
* }

I'd be interested to see what you do with the machines once you have a
list :-)

Cheers,
n


Thanks, that has helped me, but now I have another, very strange
error..
The results it returns are still pretty far wide of the mark.
I used the "LastLogon" Property and filtered out any machines over 30
days ago.
Now its still showing machines that havent been used in months/years.
And some anomalies are cropping up.

It shows the following computer names for example:
10-07
10-07-Vista

Neither of these are now valid machines, however,
When i ping these from a command prompt, they show the same IP
address.
when i ping that IP address with the -a switch to resolve the machine
name, It returns a third, completely different name.
Some PC's are ok, others are not.

This is a network/active directory problem i'm sure. But if anyone
knows why it would do this I would appreciate it.

No simple way to simply reproduce the list from Net View or My Network
Places? Those show the exact results i'm looking for..
This means that you have computers in the directory that no longer exists,
or in other words your Directory is inconsistent.
The directory is a static store, that is, it doesn't reflect the real
situation on the network. You can create a thousands of computers they will
show up even if they don't exists. The Directory does not keep track of the
actual state of a physical computer (or other managed device for that
matter), so you can't use DS to get a list of powered on computers.
What you can do is use a combination of DS and WMI, but this requires at
least a consistent Directory;
use DS to get a list of computers and use WMI to query the state of these
computers, note that this requires WMI to be enabled and accessible
remotely.
Willy.

May 23 '07 #4
On 2007-05-23 12:24:28 +0100, JB <ja*******@gmail.comsaid:
>>
The way I tackled a similar problem was to first omit any computer
objects that are disabled then calculate the likelyhood of a computer
object being "active" based on the PwdLastSet attribute. This will
narrow your search somewhat but you'll stil run into computers that are
not switched on at that moment in time. Actually, I am planning on
implementing a similar feature such that the results of the
DirectorySearcher can be fed into another interactive process and my
plan is to use a WMI ping to determine if the computer can be accessed
remotely.

narrowing the search to include only non-disabled objects:

* searcher.filter =
"(&(ObjectCategory=computer)(!UserAccountControl: 1.2.840.113556.1.4.803:=2))";

speed

up the search by only selecting the attributes you are interested in:

* searcher.propertiesToLoad.Add("cn");
* searcher.propertiesToLoad.Add("PwdLastSet");

I should say at this point I have to calculate the PwdLastSet time
because we are not running a windows 2003 domain in native mode. I
believe in 2k3 the LastLogon time stamp (or something similar) is
replicated to all DCs which will make this method redundant. If you
have a 2k3 domain I would investigate looking for that attribute.
However . . .

. . .Now calculate a suitable cut off period based on the PwdLastSet attribute

[your routine for itterating through the list of comp objects in the
searchresults]

* DateTime today = DateTime.Now;
* DateTime cutoff = today.Subtract(new TimeSpan(30,0,0,0)); //30 Days
* //I'm not sure if the following line is exactly necessary but I use
it as I need to output a friendly time
* DateTime compPwdLastSet =
DateTime.FromFileTime((long)comp.Properties["PwdLastSet"][0]);

* If (compPwdLastSet <= cutoff)
* {
* //object is probably stale
* }

I'd be interested to see what you do with the machines once you have a list :-)

Cheers,
n


Thanks, that has helped me, but now I have another, very strange
error..
The results it returns are still pretty far wide of the mark.
I used the "LastLogon" Property and filtered out any machines over 30
days ago.
Now its still showing machines that havent been used in months/years.
And some anomalies are cropping up.

It shows the following computer names for example:
10-07
10-07-Vista

Neither of these are now valid machines, however,
When i ping these from a command prompt, they show the same IP
address.
when i ping that IP address with the -a switch to resolve the machine
name, It returns a third, completely different name.
Some PC's are ok, others are not.

This is a network/active directory problem i'm sure. But if anyone
knows why it would do this I would appreciate it.

No simple way to simply reproduce the list from Net View or My Network
Places? Those show the exact results i'm looking for..
The first thing I would say is make sure your domain is running in
windows 2003 native mode. The LastLogon attribute is not replicated
prior to that so it's completely possible you will pick up stale
results.

When pinging machines with just the host name you need to make sure
your lookup options are doing what you expect them to ie. WINS, DNS and
search order etc. You might also run into situations where hosts are
running NAT locally (say for sandboxing / virtualisation reasons) which
is a potential reason for getting two hosts sharing an IP.

As for running Net View and capturing the output - I couldn't get that
to work consistently on my machine.

You can use System.Diagnostics.Process to launch and (at least by the
look of it) capture the output stream. I couldn't get that to work at
all.

using System.Diagnostics;

Process myProc = Process.Start("net","view");

Good Luck
May 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dirk Hagemann | last post: by
1 post views Thread by Lloyd Taylor | last post: by
reply views Thread by MAF | last post: by
16 posts views Thread by Terry Burns | last post: by
3 posts views Thread by Jeff Waskiewicz | last post: by
12 posts views Thread by js | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.