473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 7641
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dirk Hagemann | last post by:
Hi! I'm using this code to list all computers of our WinNT-Domains: import win32com.client from pprint import pprint as p domain='domainX' auswahl='computer' def enumerate(domain,auswahl):...
1
by: Lloyd Taylor | last post by:
Hello, I have a simple bit of code which I have found in the forums which lists all the computer(s) in a domain. The code is: DirectoryEntry domainEntry = new...
8
by: Sameh Ahmed | last post by:
Hello there I need to get a list of domain controllers to a domain I bind too using it's name rather than the DN. I am trying a query looking for objectClass=nTDSDSA but this returns 0 results I...
0
by: MAF | last post by:
How can I create a list of domain users and groups?
1
by: MAF | last post by:
How can I create a list of domain users and groups?
16
by: Terry Burns | last post by:
Hi Guys, I know this is a bit off topic, but im not having any luck googling it. I want to know where I can go programatically to get a list of all registered domains on the internet. ...
3
by: Jeff Waskiewicz | last post by:
Hello, I think the subject says it all I am trying to return a list of domain names to later display in a combobox. I found a number of VB6 samples that show how to do this using the...
12
by: js | last post by:
Hi list, I have a list of URL and I want to sort that list by the domain name. Here, domain name doesn't contain subdomain, or should I say, domain's part of 'www', mail, news and en should be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.