Solved

VBS search for ou

Posted on 2007-12-03
1
2,017 Views
Last Modified: 2009-12-16
Hi there
I have found this script cause I was searching for a script to search the ad for specific ou´s and return the path to a string.
This one searches only for users and I cant seem to find a script to search for ou´s.

My goal is to have a script that automatically discovers the ou, delivers it to a string and create the users in that ou. I have an ou in each AD that is named school, but the location in the AD´a are different.
So I´d like the script to search for an ou named school and deliver the path.
The rest of the script I have, so this is my only quarrel.
I hope the line
strADSQuery = "SELECT samAccountName, givenName, sn, AdsPath FROM 'LDAP:// " & _
strDefaultDomainNC & "' WHERE samAccountName = '" & strSearch & "'"
could be rewritten to suit my purpose as this would make it quite easy to finish the whole script.

Any help would be appreciated.

Regards
John
Option Explicit
On Error Resume Next
 
'Declare variables
Dim strSearch
Dim strAdsPath
Dim strServerName
Dim strDefaultDomainNC
Dim strADSQuery
Dim objQueryResultSet
Dim objADOConn
Dim objADOCommand
Dim objUser
Dim intCount
 
Const adStateOpen = 1
 
'Check for command line argument
If WScript.Arguments.Count < 1 Then
Do While strSearch = ""
'Prompt for search criteria
strSearch = InputBox("Please specify the search criteria", "Search AD")
'If no input...Check if user wants to exit
'If user selects Yes then exit.
If strSearch = "" Then If MsgBox("Do you wish to cancel ?", 36, _
"Search AD") = 6 Then Call Cleanup(1)
Loop
Else
'Use search criteria specified on the command line
strSearch = WScript.Arguments.Item(0)
End If
 
If strSearch = "/?" Then Call ShowUsage
 
'Get the local Computer Name
strServerName = WScript.CreateObject("WScript.Network").ComputerName
 
'Get the Default Domain Naming Context
strDefaultDomainNC = GetObject("LDAP://RootDSE").Get("DefaultNamingContext")
 
If (IsEmpty(strDefaultDomainNC)) Then
 
Wscript.Echo("Error: Did not get the Default Naming Context")
Call Cleanup(2)
End If
 
'Dispaly Server Name and Default Naming Context
 
WScript.Echo("The Computer Name is: " & strServerName)
WScript.Echo("The Domain Naming Context is: " & strDefaultDomainNC)
 
'Set up the ADO connection required to implement the search.
Set objADOConn = CreateObject("ADODB.Connection")
 
objADOConn.Provider = "ADsDSOObject"
'Connect using current user credentials
objADOConn.Open "Active Directory Provider"
 
'Code to demonstrate connecting using alternate credentials
'objADOConn.Open "", _
' "CN=Administrator,CN=Users," & strDefaultDomainNC, "password"
 
'Verify successful connection state
If objADOConn.State = adStateOpen Then
 
WScript.Echo("Authentication Successful!")
Else
Wscript.Echo("Authentication Failed.")
Call Cleanup(3)
End If
 
Set objADOCommand = CreateObject("ADODB.Command")
Set objADOCommand.ActiveConnection = objADOConn
 
'Format search criteria using SQL syntax
strADSQuery = "SELECT samAccountName, givenName, sn, AdsPath FROM 'LDAP:// " & _
strDefaultDomainNC & "' WHERE samAccountName = '" & strSearch & "'"
 
objADOCommand.CommandText = strADSQuery
 
'Execute the search
Set objQueryResultSet = objADOCommand.Execute
 
If (objQueryResultSet.EOF) Then
 
WScript.Echo("User " & strSearch & " was not found")
Call Cleanup(4)
End If
 
'Gather and echo some general information about the user
 
Wscript.Echo "Info for search criteria " & strSearch
WScript.Echo("")
 
intCount = 0
While Not objQueryResultSet.EOF
strAdsPath = objQueryResultSet.Fields("AdsPath")
WScript.Echo("The AdsPath is: " & strAdsPath)
 
intCount = intCount + 1
objQueryResultSet.MoveNext
Wend
objADOConn.Close
 
'This section is for informational purposes
'If a single user was returned, demonstrate binding to the user object
If intCount = 1 Then
Set objUser = GetObject(strAdsPath)
 
'Note: If an attribute is empty, the entire line will not echo
Wscript.Echo("sAMAccountName: " & objUser.Get("sAMAccountName"))
WScript.Echo("The Given Name is: " & objQueryResultSet.Fields("givenName"))
WScript.Echo("The Surname is: " & objQueryResultSet.Fields("sn"))
WScript.Echo("The AdsPath is: " & objQueryResultSet.Fields("AdsPath"))
Wscript.Echo("Description: " & objUser.Get("Description"))
Wscript.Echo("E-Mail: " & objUser.Get("mail"))
 
Set objUser = Nothing
End If
 
Call Cleanup (0)
 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Sub ShowUsage()
'
' Purpose: Shows the correct usage to the user.
'
' Input: None
'
' Output: Help messages are displayed on screen.
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ShowUsage()
 
Call WScript.Echo(vbCrLf & _
"Script usage: cscript SearchDC.vbs search_criteria" & vbCrLf & vbCrLf & _
"Example 1: cscript SearchDC.vbs john" & vbCrLf & _
" Will search for and return a user named john." & vbCrLf & _
"Example 2: cscript SearchDC.vbs j*" & vbCrLf & _
" Will search for and return all users with a login" & vbCrLf & _
" name begining with j." & vbCrLf & _
"Example 3: cscript SearchDC.vbs *" & vbCrLf & _
" Will search for and return all user logins, groups" & vbCrLf & _
" and computer accounts." & vbCrLf & _
"Example 4: cscript SearchDC.vbs * > results.txt" & vbCrLf & _
" Will search for all user logins and computer accounts" & vbCrLf & _
" and return the results to a file named results.txt.")
 
Call Cleanup(0)
 
End Sub
 
'Sub Cleanup
 
' Purpose: Cleanup objects and exit
Sub Cleanup(intExitCode)
 
Set objADOConn = Nothing
Wscript.Quit(intExitCode)
 
End Sub

Open in new window

0
Comment
Question by:Ducknaldi
1 Comment
 
LVL 12

Accepted Solution

by:
chandru_sol earned 125 total points
ID: 20394674
Try this script for the OU search....

Save the script has HTA and it will give you the list of OU's


regards
Chandru
<SCRIPT Language="VBScript">
    Sub Window_onLoad
        On Error Resume Next
 
        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 
 
        objCommand.CommandText = _
            "SELECT Name, ADsPath FROM 'LDAP://dc=fabrikam,dc=com'" & _
                " WHERE objectCategory='organizationalUnit' ORDER BY Name"  
        Set objRecordSet = objCommand.Execute
 
        objRecordSet.MoveFirst
        Do Until objRecordSet.EOF
            Set objOption = Document.createElement("OPTION")
            objOption.Text = objRecordSet.Fields("Name").Value
            objOption.Value = objRecordSet.Fields("ADsPath").Value
            AvailableOUs.Add(objOption)
            objRecordSet.MoveNext
        Loop
    End Sub
 
    Sub ShowOUPath
        Msgbox AvailableOUs.Value
    End Sub
</SCRIPT>
 
<body>
    <select size="10" name="AvailableOUs" style="width:300"></select><p>
    <input id=runbutton type="button" value="Show OU Path" onClick="ShowOUPath">
</body>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question