Re: Moving Multiple users from many differnt OU's at once
- From: poboy_n.o_style <gryphonfoo@xxxxxxxxxxx>
- Date: Mon, 20 Mar 2006 07:07:52 -0800
I ran the script for the Specify Common Name. That retuned the correct value
of the location to the Test Account1. User found: CN=Test Account1, OU=Users,
OU=Accounting, OU=Mydomain, OU=Com. You said something about looping this
one with the first script that had strExcelPath = "c:\scripts\Users.xls"?
Thanks Richard,
Troy.
"Richard Mueller" wrote:
Hi,.
Distinguished Names are similar to:
cn=Test Accnt1,ou=West,dc=MyDomain,dc=com
The reveal where in the AD hierarchy the object resides. We need to find out
if the names you have are Common Names or NT Names (sAMAccountName). If the
names are NT Names (or the NT Name values are always the same as the Common
Name), you can use the NameTranslate object to convert the names to
Distinguished Names. If the names are Common Names, then you need to use ADO
to search the domain for all users that have the value for Common Name.
Maybe some test scripts will help. The following assumes that "Test Accnt1"
is the NT Name. The NT Name must be combined with the NetBIOS name of the
name. Then NameTranslate can convert to the DN.
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
' Specify the NetBIOS name of the domain and the NT name of the user.
strNTName = "MyDomain\Test Accnt1"
' Use the NameTranslate object to convert the NT user name to the
' Distinguished Name required for the LDAP provider.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the NT format of the object name.
objTrans.Set ADS_NAME_TYPE_NT4, strNTName
' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
' Bind to the user object in Active Directory with the LDAP provider.
Set objUser = GetObject("LDAP://" & strUserDN)
If "Test Accnt1" is the common name, then we have to use ADO to search AD
for the user object, and hope there is only one match. The script would look
something like this:
Dim strCN, objRootDSE, strDNSDomain, objCommand, objConnection
Dim strBase, strFilter, strAttributes, strQuery, objRecordSet
Dim strDN
' Specify Common Name.
strCN = "Test Accnt1"
' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user objects with given Common Name.
strFilter = "(&(objectCategory=person)(objectClass=user)(cn=" & strCN & "))"
' Retrieve Distinguished Name.
strAttributes = "distinguishedName"
' Construct ADO query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
' Run the query.
Set objRecordSet = objCommand.Execute
If objRecordSet.EOF Then
Wscript.Echo "User not found with cn=" & strCN
Wscript.Quit
End If
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName").Value
Wscript.Echo "User found: " & strDN
objRecordSet.MoveNext
Loop
Wscript.Echo strDisplay
' Clean up.
objConnection.Close
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Code like this would have to be included in a loop where we read names from
the spreadsheet. Also, logic would be added to move the user object only if
there is only one user found with the given cn. If more than one record is
returned in the ADO recordset, do nothing because we have not identified the
user.
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
"poboy_n.o_style" <gryphonfoo@xxxxxxxxxxx> wrote in message
news:52DFB4DC-7A14-485D-ADE1-8349B76DBA9E@xxxxxxxxxxxxxxxx
I created the Users.xls
Test Accnt1
Test Accnt2
Test Notexist
I created the Test Accnt1 and Accnt2 in two differnt OU in nearly the same
places from example earlier, running script say that "User Not found" for
both accounts and of course the third since I never created it.
"Richard Mueller" wrote:
Then something like the example below should work:
Option Explicit
Dim objExcel, strExcelPath, objSheet, intRow, strUserDN
Dim objUser, objOU
' Bind to the OU that users are moved into.
Set objOU = GetObject("LDAP://ou=Terminated,dc=MyDomain,dc=com")
' Bind to Excel object.
On Error Resume Next
Err.Clear
Set objExcel = CreateObject("Excel.Application")
If Err.Number <> 0 Then
Err.Clear
MsgBox "Excel application not found."
Wscript.Quit
End If
On Error GoTo 0
' Specify spreadsheet of users.
strExcelPath = "c:\scripts\Users.xls"
' Open specified spreadsheet and select the first worksheet.
objExcel.WorkBooks.Open strExcelPath
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
' Iterate through the rows of the spreadsheet after the first, until the
' first blank entry in the first column. For each row, bind to the user
' specified in the first column.
intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
strUserDN = objSheet.Cells(intRow, 1).Value
On Error Resume Next
Set objUser = GetObject("LDAP://" & strUserDN)
If Err.Number <> 0 Then
On Error GoTo 0
' Failed to bind to user object.
MsgBox "User NOT found" & vbCrLf & strUserDN
Else
On Error GoTo 0
' Move the user object.
objOU.MoveHere objUser.AdsPath, vbNullString
End If
intRow = intRow + 1
Loop
' Close workbook and quit Excel.
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
' Clean up.
Set objExcel = Nothing
Set objSheet = Nothing
Set objUser = Nothing
MsgBox "Done"
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
"poboy_n.o_style" <gryphonfoo@xxxxxxxxxxx> wrote in message
news:2E71D580-D77F-46DA-85A6-E393CC737471@xxxxxxxxxxxxxxxx
Thank Richard,
I will have an Excel sheet with the Distinguished Names.
"Richard Mueller" wrote:
"poboy_n.o_style" <gryphonfoo@xxxxxxxxxxx> wrote in message
news:D4A24F07-062E-4659-BB87-AA41964DBF79@xxxxxxxxxxxxxxxx
Looking for a script that can use a TXT file or excel.xls file to
move
many
differnt user from many differnt OU's into one OU. Example
(TXT or XLS)
JOHN DOE
JANE SMITH
John is in the (cn=John Doe, ou=user, ou=Accounting, dc=mydomin,
dc=com)
Jane is in the (cn=Jane Smith, ou=user, ou=, dc=mydomin, dc=com)
I get up to 50 or so users at once on a spreedsheet or text file.
They both need to be moved to the (ou=terminated, dc=mydomin,
dc=com)
Any help, Please.
Hi,
We first need to uniquely identify the users. If the text file has
user
Common Names, this can be a problem. Common Names are only unique in
the
container/OU. The best you can do is use ADO (in a VBScript program)
to
search the domain for all users that have the specified value for the
cn
attribute and if there is one user object found, do what you want, but
skip
if more than one is found.
Best would be a text file or spreadsheet that either has the
Distinguished
Names or the NT Names (sAMAccountNames, or what is called the
"Pre-Windows
2000 logon name" in ADUC) of the users. Both uniquely identify the
users
in
the domain. If you have Distinguished Names, you can immediately bind
to
the
user object and move it as desired. If you have sAMAccountName you
must
use
the NameTranslate object to convert this to the Distinguished Name,
but
that's no problem. In your case, you might be lucky and the Common
Names
of
your users always matche exactly the sAMAccountName.
In ADUC the Common Name (cn attribute) is in a column called "Name".
The
sAMAccountName is on the Account tab in a field labeled User logon
name
(pre-Windows 2000).
Reply with whether you plan a text file or spreadsheet, and if the
names
will be cn, sAMAccountName, or distinguishedName. A VBScript program
can
handle this, but the code will be different depending.
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
- Follow-Ups:
- Re: Moving Multiple users from many differnt OU's at once
- From: Richard Mueller
- Re: Moving Multiple users from many differnt OU's at once
- References:
- Re: Moving Multiple users from many differnt OU's at once
- From: Richard Mueller
- Re: Moving Multiple users from many differnt OU's at once
- From: Richard Mueller
- Re: Moving Multiple users from many differnt OU's at once
- From: poboy_n.o_style
- Re: Moving Multiple users from many differnt OU's at once
- From: Richard Mueller
- Re: Moving Multiple users from many differnt OU's at once
- Prev by Date: Re: Logon script
- Next by Date: Find Users in a Group
- Previous by thread: Re: Moving Multiple users from many differnt OU's at once
- Next by thread: Re: Moving Multiple users from many differnt OU's at once
- Index(es):
Relevant Pages
|