We help IT Professionals succeed at work.

Routine to search through child ou's

pjbonc
pjbonc asked
on
I need this working script to do a little extra and drill down to the ou's under it and give me all computer accounts below the root. Right now it will return the computers in the path that I put in but I want it dynamically search all sub ou's  
On Error Resume Next 


Set objOU = GetObject("LDAP://OU=Something,OU=Something,OU=The,dc=Dark,dc=Side") 
objOU.Filter = Array("Computer") 

For Each objComputer In objOU 
	strComputer = objComputer.CN 
	' ===================================================================== 
	' Insert your code here 
	' ===================================================================== 






	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
	Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
		For Each objOperatingSystem in colOperatingSystems
		ObjOperatingSystem.Reboot()
Next
	' ===================================================================== 
	' End 
	' ===================================================================== 
Next

Open in new window

Comment
Watch Question

Here is a script I made to prompt for the OU to start the search and searches all sub OUs.

Let me know if this is what you are looking for and if you need help modifying it.
'Define Constants
Const ADS_SCOPE_BASE = 0 'Search base object only
Const ADS_SCOPE_ONELEVEL = 1 'Search one level of immediate children
Const ADS_SCOPE_SUBTREE = 2 ' Search target object and all sub levels

'Declare Variables
Dim DQ
Dim objShell
Dim objRootDSE
Dim strADsPath
Dim objConnection
Dim objCommand
Dim strOUPath
Dim objRecordSet
Dim strSearchADsPath
Dim intOULevel

'Set Variables
DQ = Chr(34) 'Double Quote

'Create Objects
Set objShell = CreateObject("Wscript.Shell")

'Verifies script was run using Cscript, and if not relauches it using Cscript
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
	objShell.Popup "Relaunching script with Cscript in 5 seconds...", 5, _
	"Script Host Message", 48
	objShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & _
	DQ & WScript.scriptFullName & DQ, 1, False
	WScript.Quit 0
End If

'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")

'Connect to Active Directory
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_ONELEVEL

'Prompt for Path to OU
Do
	strOUPath = InputBox("Please enter the path to the OU to Search " & _
	" - Seperate OUs With a \", "OU Path Input", "TopOU\SubOU")
	If strOUPath = False Then
		WScript.Quit
	End If
Loop Until strOUPath <> ""

'Split OU path by OU
strOUPath = UCase(strOUPath)
strOUPath = Split(strOUPath, "\")

'Prepare variables for search
intOULevel = 0
strSearchADsPath = strADsPath

'Search through each OU level in path provided
For intOULevel = 0 To UBound(strOUPath)
	objCommand.CommandText = "SELECT ADsPath FROM '" & strSearchADsPath & _
	"'" & " WHERE objectCategory='organizationalUnit' AND Name = '" & _
	strOUPath(intOULevel) & "'"
	Set objRecordSet = objCommand.Execute
	
	'Verify OU was found
	If objRecordSet.EOF Then
		WScript.echo "OU named " & strOUPath(intOULevel) & _
		" not found, Exiting script."
		WScript.quit
	Else
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			strSearchADsPath = objRecordSet.Fields("ADsPath").Value
			WScript.Echo strSearchADsPath 'Remove this line when done testing
			objRecordSet.MoveNext
		Loop
	End If
Next

Open in new window

Author

Commented:
it looks like it will do what i asked but i guess i forgot to mention that it needs to not prompt for any input. Where in this script would i put my code to execute? Would it go after the do until statement?
Try this version.
'Define Constants
Const ADS_SCOPE_BASE = 0 'Search base object only
Const ADS_SCOPE_ONELEVEL = 1 'Search one level of immediate children
Const ADS_SCOPE_SUBTREE = 2 ' Search target object and all sub levels

'Create Objects
Set objShell = CreateObject("Wscript.Shell")

'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")

'Connect to Active Directory
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

'strSearchADsPath = "LDAP://OU=Something,OU=Something,OU=The,dc=Dark,dc=Side"
strSearchADsPath = "LDAP://OU=USSUS,OU=RPPNA,DC=global,DC=rexam,DC=net"
objCommand.CommandText = "SELECT ADsPath,cn FROM '" & strSearchADsPath & _
"'" & " WHERE objectCategory='computer'"
Set objRecordSet = objCommand.Execute

If objRecordSet.EOF Then
	WScript.echo "No computers found, Exiting script."
	WScript.quit
Else
	objRecordSet.MoveFirst
	Do Until objRecordSet.EOF
		strComputer = objRecordSet.Fields("cn").Value
'		Your code goes here.
		
		
		
		
		
		objRecordSet.MoveNext
	Loop
End If

Open in new window