Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find which OU a computer is in 1

Status
Not open for further replies.

mooens

Technical User
Jul 12, 2005
1
DK
How do i check which OU's a computer is in.
 
Couple of ways... easy way is to dump all of the computers and then do a quick search for the one you want :)

Code:
Enumerate 
Sub Enumerate ()

	strADsPath = "LDAP://dc=your_domain,dc=com"

	' replace the above line with this one to walk the "users" sub tree
	' of the Active Directory
'	WScript.STDOUT.WriteLine "+" & strADsPath
	WalkTree strADsPath, "|-->"
End Sub

' recursive function to walk down through the tree
Sub WalkTree(strPath, buffer)
	Set oContainer = GetObject(strPath)
	
	For Each object In oContainer
		If object.Class = "computer" Then
			WScript.STDOUT.WriteLine buffer & object.Name
		Else
			Set oClass = GetObject(object.Schema)
			If (oClass.Container) and object.Schema = "LDAP://schema/organizationalUnit" Then 
				WalkTree object.ADsPath, buffer & object.Name & "| "
			End If
		End If
	Next
End Sub

Other option would be to do LDAP search, and then to use the canonical name (cn) to figure it out...

 
Give this scritp a try:

Code:
objToFind = InputBox("Enter Computer Name to Locate","What Should I Find?")


ExecuteSearch = SearchDistinguishedName(objToFind)

Public Function SearchDistinguishedName(ByVal vSAN)

Const ADS_SCOPE_SUBTREE = 2
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set objCommand = CreateObject("ADODB.Command")
    objCommand.ActiveConnection = oConnection

ldstring = "'LDAP://" & oRootDSE.get("defaultNamingContext") & "'" 

objCommand.CommandText = "Select Name, distinguishedName from "& ldstring & " where objectClass='computer'"  

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
objCommand.Properties("Cache Results") = False 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
If lcase(objRecordSet.Fields("Name").Value) = lcase(vSan) Then
    Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value & vbCrLf _ 
    & "Location: " & objRecordSet.Fields("distinguishedName").Value
    'Wscript.Quit
End If
    objRecordSet.MoveNext
Loop
End Function

I hope you find this post helpful.

Regards,

Mark
 
Hi Mark --

Just wondering why you are walking the entire directory? I ran your function against a domain with about 30,000 computer objects and it can take well over 60 seconds to find a specific object, depending on OU location. In fact, wscript.exe is spontaneously quitting on my workstation when trying some queries. I did change the timeout to 300 seconds.

I've been using a "name translate" function that was posted by Richard L. Mueller. Its virtually instantaneous since it doesn't walk the directory.

Here's the code segment I use...

Code:
Dim wshShell, wshNetwork
Dim strComputerName

' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")

' Initialize Variables
strComputerName = wshNetwork.ComputerName

wscript.echo "Computer DN: " & GetDN

Function GetDN()
' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$". Returns comma delimited string to calling code.

	Dim objTrans, objDomain
	' Constants for the NameTranslate object.
	Const ADS_NAME_INITTYPE_GC = 3
	Const ADS_NAME_TYPE_NT4 = 3
	Const ADS_NAME_TYPE_1779 = 1

	Set objTrans = CreateObject("NameTranslate")
	Set objDomain = getObject("LDAP://rootDse")
	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
	& strComputerName & "$"
	GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
	'Set DN to upper Case
	GetDN = UCase(GetDN)
End Function

Since I'm still new to scripting, what are the benefits of each method?

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top