Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1700
  • Last Modified:

batch file log dsadd errors

I'm trying to do a bulk dsadd user (which I can do) and add the samid to a log file if it was unable to add the user. I have tried adding if not errorlevel 0 command without success. I am using the following code to create the user but can't figure out how to log the failures.

for /f "delims=, tokens=1-2" %%a in (users.txt) do @dsadd user "CN=%%a,OU=theOU,DC=theDC" -samid %%a -pwd password -email %%b
1 Solution
The error processing should work. You can get into problems with delayed expansion:

@echo off

setlocal enabledelayedexpansion

for /f "delims=, tokens=1-2" %%a in (users.txt) do  (
  @dsadd user "CN=%%a,OU=theOU,DC=theDC" -samid %%a -pwd password -email %%b
  if not ERRORLEVEL 0 echo Error !errorlevel! detected
Redirect the stdout and stderr to files:

for /f "delims=, tokens=1-2" %%a in (users.txt) do @dsadd user "CN=%%a,OU=theOU,DC=theDC" -samid %%a -pwd password -email %%b  1>>out.log  2>>err.log
kirkd76Author Commented:
Thanks knight, works like a charm. Question, what do the 1 and the 2 represent in the log output?
Neadom TuckerCommented:
Can you post your script?  I am trying to figure out the rest of your batch file and I am totally clueless on this.  I have all my dsadd build in an excel file and I usually just copy and paste.  i would like a log file.  When I add the >c:\log.txt it overwrites the file each time instead of appending it.  



Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now