Re: Get SAMAccountName by Brandon
Brandon
Fri Feb 18 17:19:11 CST 2005
Ernie FErro wrote:
> Could someone tell me how to determine the SAM account name using VBScript
> and LDAP? I'm trying to write a login scrip that does stuff based on the
> users SAM account name and haven't been able to get anything I've been
> trying to work.
> TIA
> Ernie
Once you made a connection to ADS and performed a query, accessing the
sAMAccountName field is simple. Are you having trouble getting query results
from AD or just not able to get the value of the sAMAccountName from your
results?
The following VBS lines come from OReilly's ADS Cookbook. This recipe is for
creating a bunchof users at once but it should illsustrate what you need to
do(don't forget to do the .setInfo method or your changes won't stick):
6.2.2.2 Using VBScript
' This code creates a large number of users with incremented user names
' e.g. User1, User2, User3, ....
' ------ SCRIPT CONFIGURATION ------
intNumUsers = 1000 ' Number of users to create
strParentDN = "<ParentDN>" ' e.g. ou=bulk,dc=emea,dc=rallencorp,dc=com
' ------ END CONFIGURATION ---------
' Taken from ADS_USER_FLAG_ENUM
Const ADS_UF_NORMAL_ACCOUNT = 512
set objParent = GetObject("LDAP://" & strParentDN)
for i = 1 to intNumUsers
strUser = "User" & i
Set objUser = objParent.Create("user", "cn=" & strUser)
objUser.Put "sAMAccountName", strUser
objUser.Put "userAccountControl", ADS_UF_NORMAL_ACCOUNT
objUser.SetInfo
To retrieve the sAMAccountName instead of setting it just do
objUser.Get("sAMAccountName") and put it into a variable.
HTH
Brandon