尝试使用批处理将批量用户添加到活动目录时出错

时间:2015-06-26 13:25:41

标签: batch-file active-directory

我在Windows Server 2012中使用简单的Microsoft DOS批处理脚本将用户添加到Active Directory中;但是,我收到了错误

  

" DELIM =,"此时出人意料

csv文件

smith,john
page,jimmy
cooke,pane
doe,jane

DOS for loop

for /F "tokens=1,2 delim=," %%a in (C:\users.csv) do (
    set username=%%b:~0,1%%a
    dsadd user "cn=%username%,cn=users,dc=contoso,dc=com" -fn %%b -ln %%a -upn username@contoso.com -pwd Password123 
)

1 个答案:

答案 0 :(得分:0)

下一个评论的代码段可以提供帮助:

@echo OFF
SETLOCAL enableextensions enabledelayedexpansion
for /F "tokens=1,2 delims=, " %%a in (C:\users.csv) do (
    rem                 ^  ^
    set "username=%%b"
    rem e.g.      john
    set "username=!username:~0,1!%%a"
    rem e.g.                    jsmith
    dsadd user "cn=!username!,cn=users,dc=contoso,dc=com" -fn %%b -ln %%a -upn !username!@contoso.com -pwd Password123 
    rem                                                                        ^        ^
)

但是,出于调试目的,请使用echo dsadd user而不是操作dsadd user命令,如下所示:

echo dsadd user "cn=!username!,cn=users,dc=contoso,dc=com" -fn %%b -ln %%a -upn !username!@contoso.com -pwd Password123

在调试之后立即删除echo资源(必读,不完整):