Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


get CN from samaccountname

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3615
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> get CN from samaccountname
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 get CN from samaccountname - 7/2/2005 5:50:04 AM   
  Bezerk

 

Posts: 22
Score: 0
Joined: 6/20/2005
From: Netherlands
Status: offline
Hi,

I have a .cvs with four columns.

1=samaccountname, 2=ou of samaccountname, 3 is cn of group, 4 is ou of group

I want to get the cn from the samaccountname so i can get it to work.

Who has an idea?
 
 
Post #: 1
 
 Re: get CN from samaccountname - 7/2/2005 6:37:26 AM   
  tnoonan

 

Posts: 364
Score: 0
Joined: 12/14/2004
From:
Status: offline
Sub CNNT()
Set fso = CreateObject("Scripting.FileSystemObject")
Set outCNF = fso.OpenTextFile(outCNFile, ForAppending, True)

Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strBase = "<LDAP://dc=spoogenet,dc=com>"

strFilter = "(&(objectCategory=person)(objectClass=user))"
strAttributes = "sAMAccountName,cn"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

Do Until objRecordSet.EOF
strName = objRecordSet.Fields("sAMAccountName").Value

strCN = objRecordSet.Fields("cn").value
outCNF.Writeline strName & "." & strCN
objRecordSet.MoveNext
Loop

objConnection.Close
outCNF.Close
End Sub

(in reply to Bezerk)
 
 
Post #: 2
 
 Re: get CN from samaccountname - 7/2/2005 8:23:34 PM   
  Bezerk

 

Posts: 22
Score: 0
Joined: 6/20/2005
From: Netherlands
Status: offline
Thanks but that is not exactly what i am looking for, i want to autorize users bij means of samaccountname, which you can;t do because i need the users CN.

(in reply to Bezerk)
 
 
Post #: 3
 
 Re: get CN from samaccountname - 7/3/2005 6:56:18 AM   
  Bezerk

 

Posts: 22
Score: 0
Joined: 6/20/2005
From: Netherlands
Status: offline
*Kick*

Nobody an idea??

Mostly i get samaccountnames when adding users to groups, i don't want to spend time to get the CN.

So in my textfile i want to search in AD with the samaccountname and get the cn to put the user in the group.

(in reply to Bezerk)
 
 
Post #: 4
 
 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

(in reply to Bezerk)
 
 
Post #: 5
 
 RE: Re: get CN from samaccountname - 7/11/2005 12:06:41 AM   
  Bezerk

 

Posts: 22
Score: 0
Joined: 6/20/2005
From: Netherlands
Status: offline
the translate part works great i recreated the script and tested it a lot

(in reply to Xandros)
 
 
Post #: 6
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> get CN from samaccountname Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts