query computer information in vb.net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

to all,

I was wondering if anyone has sample code that I can use to get information
about a computer in active directory. For example, I want to know the date
to when a computer was joined to a domain. Here is what I'm using so far but
don't get the information that I want. Any help would be greatly
appreciated. I have looked at all the various methods for resEnt but non led
me to information about my test computer.

Dim ENTRY As System.DirectoryServices.DirectoryEntry = New
System.DirectoryServices.DirectoryEntry("LDAP://OU=Offices, DC=Test,DC=COM")

Dim MYSEARCHER As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher(ENTRY)
MYSEARCHER.Filter = ("(&(objectclass=computer)(name=testcomputer1))")
Dim resEnt As System.DirectoryServices.SearchResult

For Each resEnt In MYSEARCHER.FindAll()

Try

Console.WriteLine(":Processing:" &
Mid(resEnt.GetDirectoryEntry().Name.ToString(), 4))

Catch ex As Exception

Console.WriteLine("Trying to Connect to: " &
resEnt.GetDirectoryEntry().Name.ToString() & vbCrLf & ex.Message.ToString())

End Try

Next
 
Hi there!

You can use the properties "whenCreated" by this:

EntryObject.Properties["whenCreated"][0].ToString();

Just make sure that the whenCreated is included in the properties to load of
the Searcher.

Cheers!
 
Jon,

Thank you so much! What tool are you using to view all the possible
properties on a computer? Another property that I would like to load is the
account of who joined a computer to a domain.

Thanks again,
Michael

Jon Gonzales said:
Hi there!

You can use the properties "whenCreated" by this:

EntryObject.Properties["whenCreated"][0].ToString();

Just make sure that the whenCreated is included in the properties to load of
the Searcher.

Cheers!

Michael de Vera said:
to all,

I was wondering if anyone has sample code that I can use to get information
about a computer in active directory. For example, I want to know the date
to when a computer was joined to a domain. Here is what I'm using so far but
don't get the information that I want. Any help would be greatly
appreciated. I have looked at all the various methods for resEnt but non led
me to information about my test computer.

Dim ENTRY As System.DirectoryServices.DirectoryEntry = New
System.DirectoryServices.DirectoryEntry("LDAP://OU=Offices, DC=Test,DC=COM")

Dim MYSEARCHER As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher(ENTRY)
MYSEARCHER.Filter = ("(&(objectclass=computer)(name=testcomputer1))")
Dim resEnt As System.DirectoryServices.SearchResult

For Each resEnt In MYSEARCHER.FindAll()

Try

Console.WriteLine(":Processing:" &
Mid(resEnt.GetDirectoryEntry().Name.ToString(), 4))

Catch ex As Exception

Console.WriteLine("Trying to Connect to: " &
resEnt.GetDirectoryEntry().Name.ToString() & vbCrLf & ex.Message.ToString())

End Try

Next
 
You can loop inside the properties and get their names. Anyways for the thing
that you want to achieve right now is quite vague to me. If I do understand
you, you will have to load all the accounts that join for a specific domain?
If that's it, you don't have to get the properties of it, you just have to
adjust the AD Path you've specified before. The string that you used in the
DirectorySearcher class object.

Michael said:
Jon,

Thank you so much! What tool are you using to view all the possible
properties on a computer? Another property that I would like to load is the
account of who joined a computer to a domain.

Thanks again,
Michael

Jon Gonzales said:
Hi there!

You can use the properties "whenCreated" by this:

EntryObject.Properties["whenCreated"][0].ToString();

Just make sure that the whenCreated is included in the properties to load of
the Searcher.

Cheers!

Michael de Vera said:
to all,

I was wondering if anyone has sample code that I can use to get information
about a computer in active directory. For example, I want to know the date
to when a computer was joined to a domain. Here is what I'm using so far but
don't get the information that I want. Any help would be greatly
appreciated. I have looked at all the various methods for resEnt but non led
me to information about my test computer.

Dim ENTRY As System.DirectoryServices.DirectoryEntry = New
System.DirectoryServices.DirectoryEntry("LDAP://OU=Offices, DC=Test,DC=COM")

Dim MYSEARCHER As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher(ENTRY)
MYSEARCHER.Filter = ("(&(objectclass=computer)(name=testcomputer1))")
Dim resEnt As System.DirectoryServices.SearchResult

For Each resEnt In MYSEARCHER.FindAll()

Try

Console.WriteLine(":Processing:" &
Mid(resEnt.GetDirectoryEntry().Name.ToString(), 4))

Catch ex As Exception

Console.WriteLine("Trying to Connect to: " &
resEnt.GetDirectoryEntry().Name.ToString() & vbCrLf & ex.Message.ToString())

End Try

Next
 
Jon,

Thanks again for your response. Can you provide a sample code that would
show me how to loop out the properties of a computer? I understand what you
are saying but I don't know where to begin without a sample line of code or
two.

Once I see a sample, I can probably get the account name of who joined a
computer account to a domain.

Thanks so much again,
Michael

Jon Gonzales said:
You can loop inside the properties and get their names. Anyways for the thing
that you want to achieve right now is quite vague to me. If I do understand
you, you will have to load all the accounts that join for a specific domain?
If that's it, you don't have to get the properties of it, you just have to
adjust the AD Path you've specified before. The string that you used in the
DirectorySearcher class object.

Michael said:
Jon,

Thank you so much! What tool are you using to view all the possible
properties on a computer? Another property that I would like to load is the
account of who joined a computer to a domain.

Thanks again,
Michael

Jon Gonzales said:
Hi there!

You can use the properties "whenCreated" by this:

EntryObject.Properties["whenCreated"][0].ToString();

Just make sure that the whenCreated is included in the properties to load of
the Searcher.

Cheers!

:

to all,

I was wondering if anyone has sample code that I can use to get information
about a computer in active directory. For example, I want to know the date
to when a computer was joined to a domain. Here is what I'm using so far but
don't get the information that I want. Any help would be greatly
appreciated. I have looked at all the various methods for resEnt but non led
me to information about my test computer.

Dim ENTRY As System.DirectoryServices.DirectoryEntry = New
System.DirectoryServices.DirectoryEntry("LDAP://OU=Offices, DC=Test,DC=COM")

Dim MYSEARCHER As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher(ENTRY)
MYSEARCHER.Filter = ("(&(objectclass=computer)(name=testcomputer1))")
Dim resEnt As System.DirectoryServices.SearchResult

For Each resEnt In MYSEARCHER.FindAll()

Try

Console.WriteLine(":Processing:" &
Mid(resEnt.GetDirectoryEntry().Name.ToString(), 4))

Catch ex As Exception

Console.WriteLine("Trying to Connect to: " &
resEnt.GetDirectoryEntry().Name.ToString() & vbCrLf & ex.Message.ToString())

End Try

Next
 
Here it is, i've done it in the console app in c#, get all the objects in AD
and list all the properties for each object. Enjoy coding! Cheers!
Sorry about the code (C#), I've lost my VB.Net long time ago.

static void Main(string[] args)
{
string adPathInfo = "LDAP://SERVERNAME";
string userName = "admin", passWord = "password";
string objectFilter = "(objectClass=*)";

DirectorySearcher adSearch;
DirectoryEntry adEntry;

adEntry = new DirectoryEntry(adPathInfo, userName,
passWord, AuthenticationTypes.None);
adSearch = new DirectorySearcher(adEntry, objectFilter);
adSearch.SearchScope = SearchScope.Subtree;

SearchResultCollection results = adSearch.FindAll();
foreach (SearchResult result in results)
{
DirectoryEntry entry = result.GetDirectoryEntry();
Console.WriteLine(String.Format("AD Object Name: {0}",
entry.Name));
foreach (string prop in entry.Properties.PropertyNames)
{
Console.WriteLine(String.Format("Name: {0} Value: {1}",
prop, entry.Properties[prop][0]));
}
}
Console.ReadKey();
}
 
Back
Top