• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

Fetch all computers or users from specific OU’s.

H,

Fetch all computers or users from specific OU’s. I have 10 OU names and need to fetch all objects within it with 3 fields. Object name, Object type and OU name to excel or csv

Regards
Raja
0
bsharath
Asked:
bsharath
1 Solution
 
RobSampsonCommented:
Hi, I haven't tested this, but it should work.  It will enumerate your entire domain.  You could then just filter the CSV for the OUs you want.

Regards,

Rob.
strOutput = "DomainObjects.csv"

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

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://" & strDNSDomain & "' WHERE objectCategory='organizationalUnit'"  

Set objRecordSet = objCommand.Execute

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile(strOutput, True)
objOutput.WriteLine """OU DN"",""Object Name"",""Object Type"""

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value)
	For Each objItem in objOU
		objOutput.WriteLine """" & Mid(objOU.adsPath, 8) & """,""" & objItem.CN & """,""" & objItem.Class & """"
	Next
	objRecordSet.MoveNext
Loop

objOutput.Close
MsgBox "Done. Please see " & strOutput

Open in new window

0
 
bsharathAuthor Commented:
Can we give the OU Path or names so it fetches
As it gets 60,000+ objects and OU names are same so confusing
0
 
every1isevil2Commented:
dsquery user ou=ou name,dc=domain,dc=local -o upn >query.csv
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
every1isevil2Commented:
also your going to need to pipe it with but next half of the command will be computer
0
 
Vishal PatelCommented:
try a tool ManageEngine's AD Manager Plus. It's too good.
0
 
RobSampsonCommented:
If you want only certain OUs, change the paths to them in the array in this script.

Regards,

Rob.
strOutput = "DomainObjects.csv"

arrOUs = Array( _
	"OU=Servers1,OU=Sites,DC=domain,DC=com", _
	"OU=Servers2,OU=Sites,DC=domain,DC=com", _
	"OU=Servers3,OU=Sites,DC=domain,DC=com", _
	"OU=Servers4,OU=Sites,DC=domain,DC=com", _
	"OU=Servers5,OU=Sites,DC=domain,DC=com", _
	"OU=Servers6,OU=Sites,DC=domain,DC=com", _
	"OU=Servers7,OU=Sites,DC=domain,DC=com", _
	"OU=Servers8,OU=Sites,DC=domain,DC=com", _
	"OU=Servers9,OU=Sites,DC=domain,DC=com", _
	"OU=Servers10,OU=Sites,DC=domain,DC=com" _
	)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile(strOutput, True)
objOutput.WriteLine """OU DN"",""Object Name"",""Object Type"""

For Each strOU In arrOUs
    Set objOU = GetObject("LDAP://" & strOU)
	For Each objItem in objOU
		objOutput.WriteLine """" & Mid(objOU.adsPath, 8) & """,""" & objItem.CN & """,""" & objItem.Class & """"
	Next
	objRecordSet.MoveNext
Loop

objOutput.Close
MsgBox "Done. Please see " & strOutput

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now