Login | |
|
 |
Re: get CN from samaccountname - 7/3/2005 8:00:47 AM
|
|
 |
|
| |
Xandros
Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
|
It sounds like you're starting with the samAccountName and wanting to determine the DN (DistinguishedName) in order to add user to a group. If so, here is the original version (I think... might be one I modified) of a script I found a while back to do that. I have created variations of this code to add users to multiple groups and it seems to work fine. Hope you find it useful. ' AddToGroup2.vbs ' VBScript program to add users in a text file to a group. ' ' ---------------------------------------------------------------------- ' Copyright (c) 2002 Richard L. Mueller ' Hilltop Lab web site - http://www.rlmueller.net ' Version 1.0 - November 10, 2002 ' Version 1.1 - February 19, 2003 - Standardize Hungarian notation. ' Version 1.2 - April 18, 2003 - Remove trailing backslash from ' strNetBIOSDomain. ' Version 1.3 - January 25, 2004 - Modify error trapping. ' Version 1.4 - March 18, 2004 - Modify NameTranslate constants. ' ' This program reads user names (Distinguished Names) from a text file ' and adds the users to a group. The name of the text file and the group ' sAMAccountName are passed to the program as parameters. The program ' uses the LDAP provider to bind to the group and user objects. ' ' You have a royalty-free right to use, modify, reproduce, and ' distribute this script file in any way you find useful, provided that ' you agree that the copyright owner above has no warranty, obligations, ' or liability for such use. Option Explicit Dim objFile, objGroup, objFSO, strFile, strGroup, strUserPath, objUser Dim intCount, objRootDSE, objTrans, strNetBIOSDomain, strGroupPath Dim strDNSDomain ' Constants for the NameTranslate object. Const ADS_NAME_INITTYPE_GC = 3 Const ADS_NAME_TYPE_NT4 = 3 Const ADS_NAME_TYPE_1779 = 1 ' Check for required arguments. If Wscript.Arguments.Count < 2 Then Wscript.Echo "Required Argument Missing" & vbCrLf _ & "Syntax: cscript AddToGroup2.vbs UserList.txt GroupName" Wscript.Quit(0) End If strFile = Wscript.Arguments(0) strGroup = Wscript.Arguments(1) ' Open the text file of user names. Set objFSO = CreateObject("Scripting.FileSystemObject") On Error Resume Next Set objFile = objFSO.OpenTextFile(strFile, 1) If Err.Number <> 0 Then On Error GoTo 0 Wscript.Echo "Unable to open file " & strFile Set objFSO = Nothing Wscript.Quit(1) End If On Error GoTo 0 ' Use the NameTranslate object to get the NetBIOS domain name ' and the Distinguished Name of the group. ' Set objRootDSE = GetObject("LDAP://RootDSE") Set objTrans = CreateObject("NameTranslate") strDNSDomain = objRootDSE.Get("DefaultNamingContext") objTrans.Init ADS_NAME_INITTYPE_GC, "" objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4) ' Remove trailing backslash. strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1) objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strGroup ' Bind to the group object in Active Directory. On Error Resume Next strGroupPath = objTrans.Get(ADS_NAME_TYPE_1779) If Err.Number <> 0 Then On Error GoTo 0 Wscript.Echo "Unable to find group" & vbCrLf & strGroup objFile.Close Set objFSO = Nothing Set objFile= Nothing Set objRootDSE = Nothing Set objTrans = Nothing Wscript.Quit(1) End If Set objGroup = GetObject("LDAP://" & strGroupPath) If Err.Number <> 0 Then On Error GoTo 0 Wscript.Echo "Unable to bind to group" & vbCrLf & strGroupPath objFile.Close Set objFSO = Nothing Set objFile= Nothing Set objRootDSE = Nothing Set objTrans = Nothing Wscript.Quit(1) End If On Error GoTo 0 ' Read names from the text file, bind to the users, and add them to the ' group. intCount is the number of users added to the group. intCount = 0 Do Until objFile.AtEndOfStream strUserPath = Trim(objFile.ReadLine) If strUserPath <> "" Then On Error Resume Next Set objUser = GetObject("LDAP://" & strUserPath) If Err.Number <> 0 Then On Error GoTo 0 Wscript.Echo "User " & strUserPath & " not found" Else objGroup.Add(objUser.AdsPath) If Err.Number <> 0 Then On Error GoTo 0 Wscript.Echo "Error adding user " & objUser.sAMAccountName & " to group " & strGroup Else On Error GoTo 0 intCount = intCount + 1 End If End If End If Loop ' Save the changes to the group object. On Error Resume Next objGroup.SetInfo If Err.Number <> 0 Then On Error GoTo 0 Wscript.Echo "Error updating group membership" Else On Error GoTo 0 Wscript.Echo "Added " & intCount & " users to group " & strGroup End If ' Clean up. objFile.Close Set objFile = Nothing Set objFSO = Nothing Set objGroup = Nothing Set objUser = Nothing Set objRootDSE = Nothing Set objTrans = Nothing
|
|
| |
|
|
|
|
|