I'm using vbscript to get information about computers on my network using the Active Directory and the WMI classes. Is there a way that I can get what OU a computer is located in. It's the only piece of info that I have left to get.
Here is a short script I wrote some time ago to examine my domain... you should be able to rework it slightly to fit your needs. Note the caveat in the comment that the default Computers container will not be searched (since it isn't an OU) so you might want to deal with that... the point being that not all computers in AD need to be part of an OU.
' This script enumerates all the computers in a domain and ' lists the OU in which they're defined. Note that if a ' computer is not a member of an OU that it won't be reported ' by this script... that includes all computers which are ' in the container "Computers" (the default location when a ' computer is remotely added to the domain).
Dim iNextID iNextID = 0
Set oRootDSE = GetObject("LDAP://RootDSE") Set oDomain = GetObject("LDAP://" & oRootDSE.Get("DefaultNamingContext"))
Call EnumOUs(oDomain.ADsPath)
Sub EnumOUs(sADsPath) Set oContainer = GetObject(sADsPath) oContainer.Filter = Array("OrganizationalUnit") For Each oOU in oContainer '' WScript.Echo oOU.ADsPath Call EnumComps(oOU.ADsPath, oOU) Call EnumOUs(oOU.ADsPath) Next End Sub
Sub EnumComps(sADsPath, oOU) Set oContainer = GetObject(sADsPath) oContainer.Filter = Array("User") For Each oADobject in oContainer If oADobject.Class = "computer" Then Wscript.Echo oADobject.Name & " in OU " & oOU.ADsPath End If Next End Sub
I have a quick question tho. What do each of the sub routines do exactly? I just want to understand the code better. I guess I will call the routines and check the oADobject.Name each time and if its the right name that i'm looking for then report out and stop.
EnumOUs finds and iterates through every OU in the domain. For each one it finds, it calls EnumComps which finds and iterates through each "User" object looking for a "computer" type of entry (users and computers are mixed together as "User" objects).
DC refers to "Domain Controller" and the "DC=xxxx" entries are the nodes of your domain name.
Yes, you need to "parse" the resulting strings returned from oADobject.Name and oADobject.ADsPath to pick out what you need. Alternatively, you can use oADobject.sAMAccountName instead of oADobject.Name to return the computer name but it will have a trailing "$" appended to it.
Here is another example script that approaches your problem more directly... just be sure to change the DC values and the computer-name value you need (you have to append the "$" character to the end of your computer name). I didn't put any error checking in it so you might want to add that... if you use a computer name that can't be found you will receive an error message.
' Herẻs the revised script that searches Active Directory and reports back ' the OU where the computer account resides.
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection
When you see two objects of the same type in the path that means there is a hierarchy of one inside the other... in other words your example shows one OU as a child of another OU (i.e. a "sub-OU", if you will).