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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active directory

Status
Not open for further replies.

rdfdr78

Technical User
Jul 19, 2005
94
I`ve posted the below question in Windows 2003 Forum however it is probably best using a script however scripting is not my forte, can anyone help?

Hello,

Within AD 2003 how do you get users profile`s within an OU to update the department field with the correct field, So for instance the account`s OU anyone within this OU would automatically get assigned the correct Department field of Accounts Dept.

Help Appreciated

Richard
 
Get the users ADsPath string, bind to it using GetObject("LDAP://" & usersadspathstring)
then set the .Department property to what you want
 
I`m a complete novice to this so step by step would be good?
Never done vb scripting! or used VB for that matter.
 
see the "AD group script not getting login info" thread yesterday, i.e on the second page of this forum
 
I think this will help you.

I have a script that I use that was from markdmac which I modified slightly to update the values you need.

Just to give credit where it is due. Thank you Mark for your MANY code samples and all the time you and all the others that have been helping all of us newwbies!!

I also would like to mention for anyone who happens onto this post that for the 'regulars' here at Tek-Tips a good way to show our appreciation for all their assistance is to give a purple star. Espessially since they have most likely pulled us out of some whole or difficulty.


Code:
'==========================================================================
'
' NAME: <FindUserOU.vbs>
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 6/25/2005
'
' COMMENT: Thanks go out to Tek-Tips user K0b3 for the
'          SearchDistinguishedName function.
'
'==========================================================================
' Code assumes you have already grabbed the user login name as UserString

ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'First grab the DistinguishedName
UserDN = SearchDistinguishedName(UserString)
'Now bind to the user object
Set UserObj = GetObject("LDAP://" & UserDN)
'Find the Relative Distinguished Name (RDN)
UserRDN = UserObj.Name
'Subtract the length of the RDN plus one more for the comma
'You now have the full path to the users OU
UserOU = Right(UserDN,Len(UserDN)-Len(UserRDN)-1)

'WScript.Echo UserOU
'WScript.Echo UserRDN
[red]
'----------------------------------------------------------
' Write Organizational information into user object.
' Added to script 11-10-2006
' John Fuhrman
'----------------------------------------------------------
UserObj.Put "title", "System Administrator"
UserObj.Put "department", "Outlink Data Center Management Team"
UserObj.Put "company", "Jack Henry & Assoc."
UserObj.SetInfo
[/red]
'-----------------------------------------------------------------------------------

Public Function SearchDistinguishedName(ByVal vSAN)
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function

Thanks

John Fuhrman
Titan Global Services
 
Also, I would like to share with you the little script I used to list all the properties of the 'user' object.

Code:
Set objSchemaComputer = GetObject("LDAP://schema/user")
 
WScript.Echo "Mandatory (Must-Contain) attributes"
For Each strAttribute in objSchemaComputer.MandatoryProperties
    WScript.Echo strAttribute
Next
 
WScript.Echo VbCrLf & "Optional (May-Contain) attributes"
For Each strAttribute in objSchemaComputer.OptionalProperties
    WScript.Echo strAttribute
Next

Hope this hels out.


Thanks

John Fuhrman
Titan Global Services
 
Check out the scripted solution I did for this in the original thread thread931-1299971. It is in my opinion one of the coolest script implementations I've done. The script itself is simple code as you all know, it is the implementation to add to the functionality of Active Directory Users & Computers that I find so amazing.

John, thank you for your kind words and keeping my name in the script. Maybe it is silly but that makes me happy. :)

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
You are welcome Mark, you and you examples and assistance have been a fantastic resource and I feel that it is common curtacy to keep the originators information in the scripts I use or modify to meet my needs.

I have been an IT System Admin/engineer for 19 years now and have not had a programming cource since basic used line numbers. [afro] So I have a healthy respect for those that are good in their fields.

Wow, your example is quite cool. I stand amazed....again.

I had not thaught of useing ADSIedit. Slick indeed.

Thanks

John Fuhrman
Titan Global Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top