472,780 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 7590
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.