Solved

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

Posted on 2002-07-04
13
4,903 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 23

Expert Comment

by:naveenkohli
ID: 7136073
0
 
LVL 10

Accepted Solution

by:
Chad Smith earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now