Find user in AD and add them to group 
Author Message
 Find user in AD and add them to group

Using VBScript on win2k, how would I search through the AD for a specific
user, and then add them to a group?

Thanks,
Aaron



Sun, 27 Jun 2004 01:15:26 GMT  
 Find user in AD and add them to group
I suppose it depends on how you want to search for the user but you must
get a user object using ADSI and then add that user object to the group.
Here is a sample that will get a user directly and add it to a group (the
group is also created by the script).

Dim oDomain, oGroup, oUser

Set oUser= GetObject("LDAP://CN=Guest, CN=Users, DC=DutraDomain,
DC=testserver, DC=test")

Set oDomain= GetObject("LDAP://CN=Users, DC=DutraDomain, DC=testserver,
DC=test")
Set oGroup = oDomain.Create("Group", "CN=Group")
oGroup.Put "sAMAccountName", "Group"
oGroup.Put "Description", "Test Group"
oGroup.SetInfo

oGroup.Add oUser.ADSPath

Set oUser = Nothing
Set oGroup = Nothing
Set oDomain = Nothing

Mike Dutra
Windows Script Host Support

This posting is provided AS IS with no warranties, and confers no rights.
You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
reserved.

--------------------

Quote:

>Newsgroups: microsoft.public.scripting.wsh
>Subject: Find user in AD and add them to group
>Date: Tue, 8 Jan 2002 09:15:26 -0800

>Using vbscript on win2k, how would I search through the AD for a specific
>user, and then add them to a group?

>Thanks,
>Aaron



Sun, 27 Jun 2004 02:17:03 GMT  
 Find user in AD and add them to group
Thanks, for the info. I'd like to search through all OU's, find the user
account and add it to a specified group as you scripted.

Thanks,
Aaron

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sun, 27 Jun 2004 03:25:39 GMT  
 Find user in AD and add them to group
I have not done very many AD searches so this may not be the easiest way
but you can try this approach using ADO:

'************************************************************
Const ADSI_PROVIDER = "ADSDSOObject"
Const ADSI_PROV_NAME = "Active Directory Provider"
Dim strQuery
Dim varName
Dim lErrorNumber
Dim strTotal
Dim cmd                 'ADODB.Command
Dim lcnt
Dim tmp                 'IADsUser
Dim m_pConn             'ADODB.Connection
Dim m_recordSet         'ADODB.Recordset

Set cmd = CreateObject("ADODB.Command")
Set m_pConn = CreateObject("ADODB.Connection")
m_bLoggedOnUser = True

'
' Setup the connection
' replace <UserName> with the user ID to created the connection under
' replace <Password> with the password of the user ID
'
'
m_pConn.Provider = ADSI_PROVIDER
m_pConn.Properties("Encrypt Password") = False
m_pConn.Properties("User ID") = "<UserName>"
m_pConn.Properties("Password") = "<Password>"
m_pConn.Open ADSI_PROV_NAME

'
' Setup the command object
'

Set cmd.ActiveConnection = m_pConn

'
' Set the query string
' Substitute your server name for mdblackadder
' and your base DN for "dc=dutradomain,dc=testserver,dc=test"
' Replace <SamName> with the samAccountName to search for
'
'

strQuery = "Select AdsPath, CN  From
'LDAP://mdblackadder/dc=dutradomain,dc=testserver,dc=test' Where " & _
        "objectClass='user' And samaccountname='<SamName>'"
WScript.Echo Len(strQuery) & ":" & strQuery
cmd.CommandText = strQuery
cmd.Properties("Page Size") = 10

'
' Execute the command
'

Set m_recordSet = cmd.Execute
WScript.Echo m_recordSet.RecordCount

While (Not m_recordSet.EOF)  
        WScript.Echo m_recordSet.Fields("AdsPath")
        m_recordSet.MoveNext
Wend
'*********************************************************************

Mike Dutra
Windows Script Host Support

This posting is provided AS IS with no warranties, and confers no rights.
You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
reserved.

--------------------

Quote:


>X-Newsreader: AspNNTP 1.50 (ActionJackson.com)
>Subject: Re: Find user in AD and add them to group

>Thanks, for the info. I'd like to search through all OU's, find the user
>account and add it to a specified group as you scripted.

>Thanks,
>Aaron

>*** Sent via Developersdex http://www.developersdex.com ***
>Don't just participate in USENET...get rewarded for it!



Sun, 27 Jun 2004 04:55:59 GMT  
 Find user in AD and add them to group
<SNIP>
Quote:
> Mike Dutra
> Windows Script Host Support

> This posting is provided AS IS with no warranties, and confers no
rights.
> You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
> reserved.

</lurking>
<helpful>
Maybe you could do some updating of your tagline, as it is 2002 already?

Kind regards,

Mike
</helpful>
<lurking>



Sun, 27 Jun 2004 17:03:05 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Find user in AD and add them to group

2. Add and remove AD user to and from local group

3. Adding Users a group in AD

4. VB script: adding AD group to local administrators group

5. Add Domain Users Group to Powe Users Group in Script

6. Adding multiple users or groups to local groups

7. Adding a computer account to an AD group

8. Adding computer to global group in AD

9. AD user group with combo box?

10. Add User to AD and Set NTFS Permission - ASP

11. Script for adding address info into multiple users in AD

12. adding a user DSN connection to MS SQL server 2000 using AD GPO logon scripts

 

 
Powered by phpBB® Forum Software