How to import and export AD domain accounts

As a domain administrator, sometimes we need to add user accounts to AD domain in batches, these user accounts have some same attributes and some different attributes. If you add and set them one by one on the graphical interface, the time and manpower required will exceed the acceptable range. In general, if there are no more than 10, we can use AD user account replication to achieve. If there are more, you should consider using command line tools to import and export objects in batches. Microsoft provides two batch import and export tools by default, namely CSVDE (CSV Directory Exchange) and LDIFDE (LDAP Data Interchange Format Directory Exchange).
   Which of the above tools to choose depends on the task that needs to be done. If you need to create objects, you can use either CSVDE or LDIFDE, and if you need to modify or delete objects, you must use LDIFDE. This article does not cover importing objects using CSVDE. Instead, use another way of importing and exporting AD accounts: use the CSVDE tool to export AD accounts to a file in CSV format, then use the For statement to read the file, and use the DSADD command to add them in batches.
  
   Specific steps:
   One: Export accounts using CSVDE
   Exporting a list of existing objects using CSVDE is fairly straightforward.
   The simplest usage is:
    csvde –f ad.csv Exports Active Directory objects to a file named ad.csv. The -f switch means that the name of the output file follows.
    But it must be noted that the above usage is very simple, but the exported results may contain too many records and information that you do not want.
    For more precise export logging, the -d and -r and -l parameters can be used.
    Where: -d is used to specify a specific search location and scope
          -r is used to specify a specific search object type
          -l is used to specify the specific properties of the exported object
    Such as:
      csvde –f users.csv –d "ou=Users,dc=contoso,dc=com" –r       "(&(objectcategory=person)(objectclass=user))" –l DN,objectClass,description
    Note: If there is Chinese in the account information exported by CSVDE, there may be garbled characters, which can be solved by adding the -U parameter.
   2: Import accounts in bulk
    The first concept that needs to be clarified is that to achieve batch import of accounts, there must be a file that already includes multiple account information. Bulk import is not possible without files.
    Suppose that such a file Users.csv has been exported through the CSVDE tool before, and the content of the file is as follows
   
    Name Full Name Login Password
    zhang,three, zhangsan, three.zhang,pass01
    li, four, li four, four.li, passo2
    king, five, king five, five.wang, pass03
    liu, six, liu six, six.liu, passo4
    Zhao, Qi, Zhao Qi, seven.zhao, pass05
  
    With the file in the above format, we can use the For command to read each piece of information in the file and use DSADD to add accounts.
    The specific sentences are as follows:
    C:\>for /f "tokens=1,2,3,4,5 delims=," %a in (uses.csv) do dsadd user "cn=%c,ou=
        newusers,dc=contoso,dc=com" -samid %d -upn  [email protected]">% [email protected]-fn
        %b -ln %a -pwd %e -disabled no
    作用:将上述文件中五个帐户添加到contoso.com域,名为newusers的OU中,且默认已启用用户。
          其中:-samid为登录名
                -upn为UPN登录名
                -fn为 名
                -ln为 姓
                -pwd为 密码
              
    简单解释一下for语句
        /f 表示从文件中读取信息
        tokens表示每行使用的记号,对应于后面的变量具体的值
        delims表示每个字段之间的分隔符
 
 
 现用记事本程序制作为hellen、lycb、linda、cathy这四个用户在grfwg.local域下的sales组织单位中批量添加的csv格式文件。所添加的属性如上所示,各属性间用半角逗号分隔。这个csv文件如下所下:
DN,objectClass,sAMAccountName,displayName
"CN=HELLEN,OU=SALES,DC=GRFW,DC=LOCAL",USER,HELLEN,恩伦
"CN=LINDA,OU=SALES,DC=GRFW,DC=LOCAL",USER,LINDA,林达
"CN=RUTHY,OU=SALES,DC=GRFW,DC=LOCAL",USER,RUTHY,鲁西
以上内容在记事本程序中输入格式如图2-26所示。



图2-26  csvde命令导入文件的输入格式 

在输入导入文件中,引号和逗号只能以英文方式输入,否则会出错。千万别在Word文档中输入,再粘贴,这样就会出现许多格式问题,导致Csvde命令不能识别。如发现内容输入没错,而在实际导入时总提示出错,则建议全部重新在记事本程序中以英文模式输入(指英文或字母字符)。另外,DN部分所包括的属性项一定要用英文引号括住(一定不能用中文引号),这样它就会把这些部分当做一个表项,否则也会出现识别错误。 
Csvde命令可以直接导入txt文本格式文件,但建议转换成csv格式,只需修改文件扩展名为csv即可。转换后的文件可用Excel程序打开,打开后就是一个表格,如图2-27所示。表头就是属性项,下面每行代表一个用户账户的相应属性配置。Csvde命令就是这过这样一个类似表格的模式来识别所添加的用户属性配置的。
文件输入和保存好后,在命令提示符下即可直接运行命令,导入用户配置文件,以批量添加用户账户。基本命令与Ldifde类似,为:
Csvde –I –f c:\2.csv
导入成功后也有相应提示,如图2-28所示。此时也会在相应的“Active Directory用户和计算机”管理单元的相应组织单位中添加了以上这3个用户,如图2-29所示。



图2-27  转换成csv格式后的导入文件



图2-28  成功运行csvde命令导入文件后的提示
 
 

图2-29  新批量添加的3个用户 

尽管Csvde与Ldifde类似,但Csvde有明显的局限性:它只能使用逗号分隔的格式(.csv)导入和导出Active Directory数据。而且配置比较麻烦,经常出错,建议您使用前面介绍的Ldifde实用工具进行“修改”操作或“删除”操作。此外,您尝试导入的项目的目录名必须位于.csv文件的第一列,否则导入操作将不起作用。 
如果你所添加的用户账户名称在“Active Directory用户和计算机”管理单元相应域中已存在,则会出现错误,提示密码无法更新之类的提示,如图2-30所示。因为原用户已有密码了,现在批量添加的用户是无密码的,而csvde命令又没有更新密码的功能,所以出现这类错误。 
出现这类错误多数情况下不是因为域组策略中设定了相应的密码策略造成的。笔者经过多次实验,也把密码策略选项进行过多次更改,强制应用,如图2-30所示的错误最终没有解除,而经过仔细比较后发现,原来确实其中有一个用户账户名称在原系统已存在,更正后即没有出现上述错误了。看来还是因为账户重名,密码不能更新的原因。 


 
图2-30  用户账户重名导致的错误
 
 
 

Csvde

使用以逗号分隔 (CSV) 格式存储数据的文件从 Active Directory 导入和导出数据。还可以支持基于 CSV 文件格式标准的批处理操作。
Csvde 是命令行工具,默认情况下,此工具安装在 Windows Server 2003 上的 %windir%/system32 文件夹中。要在运行了 Windows Server 2003 的计算机上运行  csvde,请打开命令提示符,使用合适的参数键入  csvde,然后按 Enter。
如果您在运行 Windows XP Professional 的计算机上安装 Active Directory 应用程序模式 (ADAM),则也可以在该计算机上运行  csvdeCsvde 将位于 %windir%/ADAM 文件夹中。要下载 ADAM,请参阅下载中心 ( http://go.microsoft.com/fwlink/?LinkID=29359)(页面可能为英文)的“Active Directory 应用程序模式 (ADAM)”。

语法

Csvde [ -i] [ -f  FileName] [ -s  ServerName] [ -c  String1 String2] [ -v] [ -j  Path] [ -t  PortNumber] [ -d  BaseDN] [ -r  LDAPFilter] [ -p  Scope] [ -l  LDAPAttributeList] [ -o  LDAPAttributeList] [ -g] [ -m] [ -n] [ -k] [ -a  UserDistinguishedName Password] [ -b  UserName Domain Password]

参数

-i
指定导入模式。如果没有指定,默认模式为导出。
-f FileName
识别导入或导出文件名。
-s ServerName
指定域控制器执行导入或导出操作。
-c String1 String2
将所有  String1 项替换为  String2。从一个域将数据导入到另一个域以及导出域的可分辨名称 ( String1) 需要替换为导入域的可分辨名称 ( String2) 时普遍使用该操作。
-v
设置 verbose 模式。
-j Path
设置日志文件位置。默认路径为当前路径。
-tPortNumber
指定轻型目录访问协议 (LDAP) 端口号。默认 LDAP 端口为 389。全局编录端口为 3268。
-dBaseDN
为数据导出设置搜索库的可分辨名称。
-rLDAPFilter
为数据导出创建 LDAP 搜索筛选器。
-pScope
设置搜索范围。搜索范围选项为 Base、OneLevel 或 SubTree。
-lLDAPAttributeList
设置返回至导出查询结果中的属性列表。属性可由 LDAP 按任何顺序返回,CSVDE 不尝试进行任何列排序。如果省略该参数,则返回所有属性。
-oLDAPAttributeList
设置要从导出查询结果中省略的属性列表。从 Active Directory 中导出对象然后将对象导入到另一符合 LDAP 的目录中通常使用该方法。如果另一个目录不支持属性,您可以使用该选项从结果集中忽略属性。
-g
忽略分页搜索。
-m
忽略仅适用于 Active Directory 对象的属性,如 ObjectGUID、objectSID、pwdLastSet 和 samAccountType 属性。
-n
忽略二进制值导出。
-k
在导入操作期间忽略错误并继续处理。以下是可忽略错误的完整列表:“对象已经存在”、“约束冲突”和“属性或值已经存在”。
-aUserDistinguishedName Password
将该命令设置成使用提供的  UserDistinguishedName 和  Password 来运行。默认情况下,将使用当前登录到网络的用户的凭据运行该命令。此操作对用户名和密码进行了简单的 LDAP 绑定。
-bUserName Domain Password
将命令的运行方式设置为  UsernameDomainPassword。默认情况下,将使用当前登录到网络的用户的凭据运行该命令。此操作将 LDAP 与 NEGOTIATE 身份验证方法安全地绑定在一起。
-?
显示命令菜单。

备注

  • 不能使用 csvde 导入用户密码,因为密码必须通过加密通道发送。Csvde 不支持安全套接字层 (SSL) 或加密 LDAP 通信。前面的密码参考与运行 csvde 命令的用户的凭据相关。与用户的设置密码无关。

  • 诸如 Microsoft Excel 这样的应用程序都可读取或保存 CSV 格式的数据。您还可以使用记事本创建 CSV 文件;务必使用逗号来分隔添加到文件中的值。此外,与其他许多非 Microsoft 的工具一样,Microsoft Exchange Server 管理工具也能使用 CSV 格式导入和导出数据。 

    CSV 格式由一行或多行数据组成,每个值用逗号分隔,且逗号与下一条目之间无空格。CSV 文件的第一行(有时指标题)必须包含每个属性的名称,其顺序与第一行之后的任何一行的数据顺序相同。例如:

    CN,FirstName,SurName,Description

    FirstUserLogonName,1stUserFirstName,1stUserSurname,Manager

    SecondUserLogonName,2ndUserFirstName,2ndUserSurname,President

  • 要查看可使用 csvde 进行更新的属性列表,请参阅 LDAP 的 ADSI 对象中相应的支持接口 (http://go.microsoft.com/fwlink/?LinkId=91123)(页面可能为英文)。例如,要查看可对 Active Directory 用户对象设置的属性,请单击 IADsUser 链接 (http://go.microsoft.com/fwlink/?LinkId=91124)(页面可能为英文),然后查看“属性”表。

  • 可以使用 csvde -r 为数据导出创建 LDAP 搜索筛选器。例如,以下筛选器将导出具有特定姓名的所有用户:

    csvde -r (&(objectClass=User)(sn=Surname))

示例

下列示例文件内容适用于具有组织单位 (OU) SW Dev、Acct 和 AP 并且名为 Cpandl.com 的域。AP OU 从属于 Acct OU。文件的第一行定义将由文件其余行中的条目创建的用户帐户的 Active Directory 对象属性。其余的行用于创建用户帐户。第一个用户帐户在默认的“用户”容器中创建,其余用户帐户则分别在 SW Dev、Acct 和 AP OU 中创建。
objectClass,dn,sAMAccountName,userPrincipalName,userAccountControl
user,"CN=KMyer,CN=Users,DC=cpandl,DC=com",KenM,[email protected],514
user,"CN=WYu,OU=SW Dev,DC=cpandl,DC=com",WeiY,[email protected],514
user,"CN=JMorris,OU=Acct,DC=cpandl,DC=com",JonM,[email protected],514
user,"CN=YXu,OU=AP,OU=Acct,DC=cpandl,DC=com",YeX,[email protected],514
noteNote
将 userAccountControl 设置为 514 可禁用用户帐户。由于 CSVDE 不能设置密码,因此建议使用此设置。
-d 开关表示特定查询的根(顶级)。例如,如果想要将 Contoso.com 域的 Marketing 顶级 OU 中的所有对象导出到名为 marketingobjects.csv 的文件中,您可以使用下列命令:
csvde -d "ou=marketing,dc=contoso,dc=com" -f marketingobjects.csv
-r 开关是用于从目录中导出信息的筛选器。此开关筛选出导出请求产生的输出内容。例如,如果只想将域中的用户帐户对象属性导出到名为 usersonly.csv 的文件中,您可以使用下列命令:
csvde -r objectClass=user -f usersonly.csv
下列示例将 Active Directory 数据导出到名称为 search.txt 的文件,将搜索范围设置为子树,并为在搜索中找到的每个对象列出了  sAMAccountNameCN 和  distinguished name 属性:
csvde -f search.txt -p subtree -l SamAccountName,CN,Distinguishname
下列示例从名称为 input.csv 的文件的当前域(您登录到的域)中导入数据。
csvde -i -f input.csv
下列示例从名称为 output.csv 的文件的当前域(您登录到的域)中导出数据。
csvde -f output.csv
有关使用 CSVDE 的其他示例,请参阅 Microsoft 知识库文章 327620 ( http://go.microsoft.com/fwlink/?LinkId=91125)(页面可能为英文)。

格式图例

 

格式 意义
斜体
您提供的信息
粗体
必须按照显示准确键入的元素
省略号 (...)
可在命令行中重复多次的参数
在括号 ([]) 之间
可选项目
在大括号 ({}) 之间;将选项用管道符 (|) 隔开。例如:{even|odd}
必须从中只选择一个选项的选项组
Courier font
代码或程序输出
 
 
 
 
 
 
在AD域中用CSVDE批量添加用户
2009-12-23 09:12
DN,objectClass,sAMAccountName,userPrincipalName,displayName,userAccountControl
"CN=张某某,OU=管理,DC=ceson,DC=co",user,zhang,[email protected],张某某,514
"CN=张某,OU=管理,DC=ceson,DC=co",user,lica,[email protected],张某,514
"CN=张某,OU=办公室,DC=ceson,DC=co",user,wang,[email protected],张某,514
"CN=杨某,OU=办公室,DC=ceson,DC=co",user,ly,[email protected],杨某,514
"CN=路某某,OU=办公室,DC=ceson,DC=co",user,ljj,[email protected],路某某,514
"CN=黄某某辉,OU=业务部,DC=ceson,DC=co",user,huan,[email protected],黄某某,514
注意检查各属性之间的逗号是单字节“,”不是双字节的“,”
OU要已经在域中建立才可,不然不能导入
CSVDE -i -f users.txt

本文转自q狼的诱惑 51CTO博客,原文链接:http://blog.51cto.com/liangrui/364897,如需转载请自行联系原作者
.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324285528&siteId=291194637