batch file log dsadd errors

Posted on 2007-07-23
Last Modified: 2013-01-10
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
Question by:kirkd76
    LVL 30

    Expert Comment

    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
    LVL 33

    Accepted Solution

    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

    Author Comment

    Thanks knight, works like a charm. Question, what do the 1 and the 2 represent in the log output?
    LVL 6

    Expert Comment

    by:Neadom Tucker
    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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    A quick step-by-step overview of installing and configuring Carbonite Server Backup.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now