我在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
)
答案 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
。 资源(必读,不完整):
!username!
特殊页面)EnableDelayedExpansion !username:~0,1!
)Extract part of a variable (substring)