[green]
'==========================================================================
'
' NAME: SetDepartmentFromOU.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' COPYRIGHT (c) 2006 All rights reserved
' DATE : 11/10/2006
'
' COMMENT:
'[/green][red]
' This is one cool script. To implement it do the following.
' 1. Place this script in the same folder on the hard drive of all domain controllers
' 2. Open ADSIEdit
' 3. Connect to the Configuration Context
' 4. Expand out Configuration/Configuration/Display Specifiers/CN=409
' 5. Right Click CN=organizationalUnit-Display Choose properties
' 6. Click Admin Context Menu
' 7. Click Edit
' 8. Type [b]3,Set Department,C:\Scripts\SetDepartmentFromOU.vbs[/b]
' (note to change the path to the folder and name to match your script location)
' The number 3 indicates the position the menu item will appear. If you want it first
' you can rename the other entries first to increment their menu orders then add this line
' 9. Click Add, OK, OK
' 10. Close ADUC and ADSIEdit
' 11. Open ADUC and right click the OU you wish to set Department value for. Select Set Department.
'[/red][green]
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS
' BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
' DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
' WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
' ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
' OF THIS CODE OR INFORMATION.
'=====================================
[/green]
On Error Resume Next
strDN = Wscript.Arguments(0)
strDepartment = InputBox("Enter Department Name", "Department Name to Set")
[green]
' Use ADO to search the OU for users.[/green]
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
strFilter = "(&(objectCategory=person)(objectClass=user))"
strQuery = "<" & strDN & ">;" & strFilter _
& ";distinguishedName;subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
[green]
' Enumerate users. Grab each user's Distinguished Name [/green]
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
strUserDN = objRecordSet.Fields("distinguishedName")
[green] 'Bind to the user and set the department[/green]
Set objUser = GetObject("LDAP://" & strUserDN)
objUser.Put "department", strDepartment
objUser.SetInfo
objRecordSet.MoveNext
Loop
MsgBox "Department Value Set for " & strDepartment & " users."