Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fun with FOR

Status
Not open for further replies.

billieT

MIS
Dec 11, 2001
107
0
0
NZ
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.

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!
 
The batch pipe is notorious for losing things. Have you ever considered moving to Powershell? Microsoft is.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top