?
Solved

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

Posted on 2002-07-04
13
Medium Priority
?
4,921 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 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