All I want to do is verify what users are valid in our domain. I have a nice list of usernames in a text file. So I thought a nice single-liner would do the job.
Unfortunately, it seemed that all my users were valid! (They aren't)
So, running individual lines:
Output is 0 - oops.
However, if I run the commands separately:
{Error message}
Output is 2. Which is correct.
If I run the same sequence with the two separate commands and a valid user, I get the expected output of 0.
So, why is my single-liner spuriously returning 0 even when the command result should have an errorlevel of 2? This is driving me crackers.
I have to say that it's been over 8 years since I wrote a batch job, but I did think this would be a no-brainer - I'm sure it is for some!
Code:
for /f %i in (c:\test.txt) do net user %i /domain >nul 2>&1 | if %errorlevel%==0 echo %i >> c:\valid.txt
Unfortunately, it seemed that all my users were valid! (They aren't)
So, running individual lines:
Code:
for %i in (invalid_user) do net user %i /domain >nul 2>&1 | echo %errorlevel%
Output is 0 - oops.
However, if I run the commands separately:
Code:
for %i in (invalid_user) do net user %i /domain >nul 2>&1
{Error message}
Code:
echo %errorlevel%
Output is 2. Which is correct.
If I run the same sequence with the two separate commands and a valid user, I get the expected output of 0.
So, why is my single-liner spuriously returning 0 even when the command result should have an errorlevel of 2? This is driving me crackers.
I have to say that it's been over 8 years since I wrote a batch job, but I did think this would be a no-brainer - I'm sure it is for some!