Solved

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

Posted on 2002-07-04
13
4,901 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

22 Experts available now in Live!

Get 1:1 Help Now