?
Solved

ADSI, looping throught computers in a Domain using VB .NET

Posted on 2002-07-04
13
Medium Priority
?
4,927 Views
Last Modified: 2007-11-27
I have written the class below in VB, but when i come to compile it i get the following error with the FOR loop i have constructed.

"Expression is of type 'ActiveDs.IADsDomain', which is not a collection type."

i dont understand why, can someone please help, pehaps i have done something else wrong

THANKS

'THIS IS THE CLASS

Imports ActiveDs
Public Class clsADSI


    Public Function GetAllComputers(Optional ByVal strDomain As String = "Workgroup") As String

        Dim objPDC As IADsDomain
        Dim objComputer As IADsComputer
        Dim strVal As String

        objPDC = GetObject("WinNT://" & strDomain)
        objPDC.Filter = "Computer"

        strVal = objPDC.objComputer()
'''''PROBLEM IS HERE''''''''
        For Each objComputer In objPDC
            strVal = strVal & objComputer.Name & "|"
        Next
'''''''''''''''''''''''''''''
        Return strVal

    End Function

End Class


'THIS IS THE BUTTON ON THE FORM THAT CALLS THE METHOD IN THE CLASS

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim objADSI As New clsADSI()
        Dim strval As String

        strval = objADSI.GetAllComputers()

    End Sub
0
Comment
Question by:Molko
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7130684
Here is the code that you will need to enumerate all the computers in the domain.

Dim strVal As String
strDomain = "www.foo.com"
strDomain = "WinNT://" & strDomain

' Create the Domain object
Set TheDomain = GetObject(strDomain)

' Search for Computers in the Domain
TheDomain.Filter = Array("Computer")

For Each Computer In TheDomain
strVal = strVal & Computer.Name & "|"
Next

' Clean up
Set Computer = Nothing
Set TheDomain = Nothing
0
 

Author Comment

by:Molko
ID: 7131268
Thats VB6 and not VB .NET, the above wont work in VB .NET, anyways what i have done is very simimilar anyway
0
 

Expert Comment

by:Wardall
ID: 7131773
The object you are using is not a collection base object. So you cannot use the "For Each ...". Try converting the
objPDC into a collection base :

CType(objPDC, System.Collections.ICollection)
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:Molko
ID: 7131829
I tried what you said - see below and i get a 'specified cast is invalid' at run time.

Is there another way ?


For Each objComputer In CType(objPDC, System.Collections.ICollection)

            strVal = strVal & objComputer.Name & "|"

        Next objComputer

0
 

Expert Comment

by:Wardall
ID: 7131865
Maybe there another method or property that return a collection of computers (The "Get" method ?).
0
 

Author Comment

by:Molko
ID: 7131875
Well if there is then i cant see it. I dont really know much about the ADSI and associated methods/propertys
0
 

Author Comment

by:Molko
ID: 7131885
BTW the code i originally wrote works fine under VB6, but it wont work under VB.NET ? STRANGE
0
 

Expert Comment

by:Wardall
ID: 7135018
The API have change a lot between VB6 and VB.Net. Now there must be another way to enumerate the computers on a domains with the CRL.
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7135161
If you are going to use .Net, then use System.DirectoryServices namespace classes to get this job done. Following links will give you idea on how to use these classes. You can extend these examples to enumerate the computers on your system.

Naveen
0
 

Author Comment

by:Molko
ID: 7135418
naveenkohli........what links ?
0
 
LVL 10

Accepted Solution

by:
Chad Smith earned 150 total points
ID: 7141818
Here is a c# example that retrieves the name of all computer and domain controllers from an active directory domain.  It would not be hard to convert to vb.net.

using System;
using System.DirectoryServices;

class Computers {
 public static void Main() {
  // Use the domain name to bind when trying to obtain a container (computers, builtin, ForeignSecurityPrincipals, users, system..)
  DirectoryEntry container = new DirectoryEntry("LDAP://domainName/CN=Computers,DC=domainName,DC=com",
"userName", "password");
  DirectoryEntries computerEntries = container.Children;

  foreach(DirectoryEntry deObject in computerEntries)
  {
     PropertyCollection pcoll = deObject.Properties;
     try
     {
          Console.WriteLine((pcoll["cn"])[0].ToString());
     }
     catch(System.IO.FileNotFoundException)
     {
     }
     container.Close();
   }


/* Retrieve Domain Controllers */
  container = new DirectoryEntry("LDAP://domainName/OU=Domain Controllers,DC=domainName,DC=com",
"userName", "password");
  computerEntries = container.Children;

  foreach(DirectoryEntry deObject in computerEntries)
  {
     PropertyCollection pcoll = deObject.Properties;
     try
     {
          Console.WriteLine((pcoll["cn"])[0].ToString());
     }
     catch(System.IO.FileNotFoundException)
     {
     }
     container.Close();
   }
 }
}

0
 
LVL 1

Expert Comment

by:gregjroberts
ID: 24849516
Have to admit i'm not a massive expert but have had the same problem!

Original Code (Cut down version!)
Dim TheDomain As ActiveDs.IADsDomain 'Declare the variable as a domain
TheDomain = GetObject(strDomain) (From a combobox of domains)
strDomain = Combo1.Text
strDomain = "WinNT://" & strDomain
For	Each Computer In TheDomain
List1.Items.Add(Computer.Name)
Next Computer
 
New Code (well new declare! - as the rest worked)
Dim TheDomain As New Microsoft.VisualBasic.Collection()
 
 
Technically it worked..... albeit messy!
 
I was unable to do some things such as .filter with the new code - but simply sorted those by learning a bit about collections (And actually doing it a different way)

Open in new window

0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question