2016-09-21 37 views
0

我已經嘗試了幾件事情,但我似乎無法讓我的.bat文件提供正確的輸出並讀取第一個輸入輸入的行。任何幫助將不勝感激。我正在嘗試在Active Directory中創建此批量帳戶。我的.bat文件將不會給出正確的輸出,並在讀取第一個輸入後停止。

@echo off 
echo ---- >C:\NewUserCreation\createdusers.txt 

setlocal 
FOR /F "eol=; tokens=1-12* delims=~" %%i in (C:\NewUserCreation\newusers.txt) do (

set "_lastname=%%i" 
set "_firsname=%%j" 
set _QWERTY=%%k 
call set _midinit=%%_QWERTY:~0,2%% 
set _edipi=%%l 
set _ptc=%%m 
set _ranktitle=%%n 
set _subcomponent=%%o 
set _unit=%%p 
set _position=%%q 
set _email=%%r 
set _descr=%%s 

call :do_user 
) 
endlocal 
notepad C:\NewUserCreation\createdusers.txt 
goto :EOF 


:do_user 
if "%_midinit%"=="%_edipi%" goto :no_middlename 
set _samacctname1=%_firsname%.%_midinit%.%_lastname% 
set _userPrincipalName=%_edipi% 
call :find_existing_user 
call set _samacctname=%_samaccountname% 
call set _lastinit=%%_lastname:~0,1%% 



C:\NewUserCreation\admod -h mydomaincontroller -add -b "cn=%_lastname%\, %_firsname% %_midinit%,ou=NewUsers,ou=n,ou=n,dc=n,dc=n,dc=n,dc=n" objectclass::user "sn::%_lastname%" "givenname::%_firsname%" "initials::%_midinit%" "personaltitle::%_ranktitle%" "company::%_subcomponent%" "department::%_unit%" "PhysicalDeliveryOfficeName::%_position%" "title::%_position%" "employeetype::%_ptc%" "displayname::%_descr%" "description::%_descr%" "samaccountname::%_samacctname%" "st::Anywhere,US" "streetAddress::%_unit%" "postalCode::36805" "countryCode::111" "c::USA" "co::United States" "l::Corporation" "telephoneNumber::DSN 318-872-XXXX" "userPrincipalName::%_edipi%@domain" "userPassword::password" 

if /I %ERRORLEVEL% EQU 0 (
set _resultcode=Success 
if /I %_samnametest% EQU 274 (call set _resultcode=%%_resultcode%%-DUP) 
) ELSE set _resultcode=-ERROR- 

@echo on 

sleep 15 

net user %_samacctname% /DOMAIN /passwordreq:yes 


C:\Windows\System32\dsquery * "ou=n,ou=n,dc=n,dc=n,dc=n,dc=n" -filter "(&(objectclass=user)(samaccountname=%_samacctname%))" -s mydomain |exchmbx -me SMTP:%_email%" 

@echo off 

echo %_resultcode%. . .  %_samacctname%  Contact: SMTP:%_email% >>C:\NewUserCreation\createdusers.txt 
goto :EOF 




:find_existing_userz 
set _search4Name=%_samacctnamez% 
call set _samNameSearchz=%%_search4Name:~0,20%% 
call set _samNameSearchzv=%%_search4Name:~0,16%%274 

C:\Windows\System32\dsquery * "ou=n,ou=n,dc=n,dc=n,dc=n,dc=n" -filter "(&(&(samaccountname=%_search4Name%)(ObjectClass=User))(!objectClass=computer))" -attr samaccountname|find /I "%_samNameSearchz%" >"%_samNameSearchz%.tmp" 

call :samnamefilesizez %_samNameSearchz%.tmp _samNameSearchsizez 
del %_samNameSearchz%.tmp 
if /I %_samNameSearchsizez% LEQ 1 (set _samaccountnamez=%_samNameSearchz%) ELSE (
set _samaccountnamez=%_samNameSearchzv% 
set _lastnamez=%_lastnamez%274 
) 
echo resulting_sam_account_name %_samaccountnamez% 
call set _samnametestz=%_samaccountnamez:~-3% 
goto :EOF 



:find_existing_user 
set _search4Name=%_samacctname1% 
call set _samNameSearch=%%_search4Name:~0,20%% 
call set _samNameSearchv=%%_search4Name:~0,16%%274 

C:\Windows\System32\dsquery * "ou=n,ou=n,dc=n,dc=n,dc=n,dc=n" -filter "(&(&(samaccountname=%_samNameSearch%)(ObjectClass=User))(!objectClass=computer))" -attr samaccountname|find /I "%_samNameSearch%" >%_samNameSearch%.tmp 

call :samnamefilesize %_samNameSearch%.tmp _samNameSearchsize 
del %_samNameSearch%.tmp 
if /I %_samNameSearchsize% LEQ 1 (set _samaccountname=%_samNameSearch%) ELSE (
set _samaccountname=%_samNameSearchv% 
set _lastname=%_lastname%274 
) 
echo resulting_sam_account_name %_samaccountname% 
call set _samnametest=%_samaccountname:~-3% 
goto :EOF 

:samnamefilesize 
set %2=%~z1 
goto :EOF 

:samnamefilesizez 
set %2=%~z1 
goto :EOF 




pause 
+2

由於您正在處理'for'循環和env vars,因此您應該閱讀[delayedexpansion](http://ss64.com/nt/delayedexpansion.html)。此外,代碼似乎太長(不容易遵循)。 – CristiFati

+0

如果您是批處理新手,請嘗試使用較少量的代碼。你想做什麼樣的賬戶? – Jonas

+0

請閱讀本幫助主題:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)!請至少閱讀[旅遊網頁](http://stackoverflow.com/tour)瞭解本網站的工作原理! – aschipfl

回答

0

我建議你電話 ING:do_user,那就是荷蘭國際集團:no_middlename。我只能猜測沒有:no_middlename將它返回到原始for循環中的調用。