Avatar of erobby
erobbyFlag for United States of America

asked on 

No output from command

The following command does a DSquery to get a list of computer names from AD which are then filtered using the find /v and after the filter the remaining host are pinged and "Reply From" is searched for.  If "Reply From" is found then Errorlevel is 0 indicating successful completion of the command.  I can run the command but the files aren't being output.  Everything else works correctly the second command works properly and outputs the files but the first one does not output files.

First Command:
for /f %a in ('dsquery computer -o rdn -limit 10000 ^| find /v /i "at0" ^| find /v /i "braboc" ^| find /v /i "branet" ^| find /v /i "vt0" ^| find /v /i "test"') do for /f %b in ('ping -n 1 %a ^| find "Reply From"') do if %errorlevel% GEQ 1 (echo %a >> c:\notup.txt) else echo %a >> c:\good.txt

Second Command:
for /f "tokens=1 delims=\" %a in ('net view ^| find /v /i "at0" ^| find /v /i "braboc" ^| find /v /i "branet"') do for /f %b in ('ping -n 1 %a ^| find /i "Reply"') do if %errorlevel%==1 (echo %a >> c:\notup.txt) else echo %a >> c:\good.txt
Microsoft DOS

Avatar of undefined
Last Comment
erobby
Avatar of knightEknight
knightEknight
Flag of United States of America image

for /f %a in ('dsquery computer -o rdn -limit 10000 ^| find /v /i "at0" ^| find /v /i "braboc" ^| find /v /i "branet" ^| find /v /i "vt0" ^| find /v /i "test"') do for /f %b in ('ping -n 1 %a') do if %errorlevel% GEQ 1 (echo %a ^>^> c:\notup.txt) else (echo %a ^>^> c:\good.txt)
Avatar of knightEknight
knightEknight
Flag of United States of America image

scratch that, I left out the  Find "Reply From" , which is both necessary to do what you want, and I believe is the root of the problem somehow.
Avatar of knightEknight
knightEknight
Flag of United States of America image

Try changing "Reply From" to just "Reply" in your original query.
Avatar of AmazingTech
AmazingTech

Actually your second command also does not work as you intended it to. You have computer names but they are all in the same file whether or not they are actually online.

The problem is if %errorlevel% == 1. The %errorlevel% is only evaluated when you execute your command and will not change within your for loop.

TEST1: (type in this before running your second command)

DIR Findafilethatdoesnotexisttoseterrorlevel1.txt

now run your second command.

You'll find all your computers from net view in C:\notup.txt


TEST2: (type in this before running your second command)

DIR

now run your second command.

You'll find all your computers from net view in C:\good.txt




Let's take a look at your first command in depth. The first command breaks into 3 separate parts.

1) for /f %a in ('dsquery computer -o rdn -limit 10000 ^| find /v /i "at0" ^| find /v /i "braboc" ^| find /v /i "branet" ^| find /v /i "vt0" ^| find /v /i "test"') do
2) for /f %b in ('ping -n 1 %a ^| find "Reply From"') do
3) if %errorlevel% GEQ 1 (echo %a >> c:\notup.txt) else echo %a >> c:\good.txt

Your trying to chain the output from 1) to 2) then from 2) to 3).

"1" Will output the Full Distinguished Name of the AD object. So you will see something like "CN=MyComputer,OU=rdn,DC=MyCO,DC=Com" of course if there was space it would only show the first part before the space.
So you can't ping this. You want to get MyComputer out. So "1" will need to have "tokens=2 delims=,=" it should look like

for /f "tokens=2 delims=,=" %a in ('dsquery computer -o rdn -limit 10000 ^| find /v /i "at0" ^| find /v /i "braboc" ^| find /v /i "branet" ^| find /v /i "vt0" ^| find /v /i "test"') do

"2" as it is will always be blank. The ping returns "Reply from" the 'f' is lowercase. So either add a /i to the find command or change the "Reply from".  This command will only pass the output of online workstations to "3". You do not need to do a find since ping will set errorlevel = 1 if the workstation is offline.

But because "3" doesn't work in a for command. We'll need to change "2" to not use for.


So you absolutely want this on 1 command line?
Avatar of erobby
erobby
Flag of United States of America image

ASKER

All are good suggestions.  Actually the second does work I just figured out that the net view command only shows computers that are on-line at that time so they would all responde to ping.  Computers not on-line will not show up in the net view command, that's why I chose to query AD instead.

Amazing -o rnd is relative distingushed name so you only get the CN.  Also I've done GEQ and == in the errorlevel statement.  Since it is a single command the output is shown on the screen and I can see the errorlevel values change so it does evaluate it for every instance.  Also it's a basic for command which forces evaluation on each variable instance.

Knight you have a very interesting point with the carrots I will try tomorrow and update you.

Like I said both you guys bring up excellent points.  I know it would have been easier just to script it in a batch file, but I've been experimenting with the one command option and finding it quite fun.  Not for any particular reason just something to do at work to kill the boredom while I still produce data.
Avatar of AmazingTech
AmazingTech

I still don't get the IF %ERRORLEVEL% changing.

Try this adding /i

for /f %a in ('dsquery computer -o rdn -limit 10000 ^| find /v /i "at0" ^| find /v /i "braboc" ^| find /v /i "branet" ^| find /v /i "vt0" ^| find /v /i "test"') do for /f %b in ('ping -n 1 %a ^| find /i "Reply From"') do if %errorlevel% GEQ 1 (echo %a >> c:\notup.txt) else echo %a >> c:\good.txt
ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of erobby
erobby
Flag of United States of America image

ASKER

Thanks for the try.  Still not resolved but like I saidd i was playing to see if how far I could push DOS
Microsoft DOS
Microsoft DOS

Microsoft Disk Operating System (MS-DOS) was an operating system for x86-based personal computers, and traces of it are still found in the Windows operating system. DOS is still used in some embedded systems and for certain legacy 16-bit networks.

14K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo