A one-time use script.

This isn’t the entire thing, it’s bits and pieces, but it’s enough for you to bend, fold, spindle, and mutilate for your own purposes.

I was given 70,000+ tab delimited lines that were output dumped from a program and asked to find out what version of a specific piece of software was running on the servers in position 9 of those lines.  I was also told that there were duplicates and that some of them were Unix or Linux and I was only responsible for the Windows servers. I used batch because it was there and available.

First of all, batch choked on the tabs so I replaced all tabs with spaces, then picked out the server names and sorted them:

SetLocal EnableDelayedExpansion

for /f “usebackq delims=” %%g in (%controlfile%) do (
set content=%%g
echo !content:  = ! >> process.txt

for /f “tokens=9 delims= ” %%A in (process.txt) do (
echo %%A >> servers.txt

sort < servers.txt > sorted.txt

I then had 70,000 server names with a lot of duplicates in them.  Next up:  Unduplicating them.

@echo on > unduped.txt

for /f “tokens=* delims= ” %%a in (sorted.txt) do (
find “%%a” < unduped.txt > nul
if errorlevel 1 echo %%a >> unduped.txt

That gave me 110 (!) server names.  Next, to see which ones are behind a firewall or otherwise unreachable:

@echo on > unreachable.txt

for /f “usebackq delims=” %%g in (unduped.txt) do (
for /f “tokens=11 delims= ” %%A in (‘ping %%g ^| FIND “loss)”‘) do (
if “%%A” NEQ “(0%%” (
echo %%g >> unreachable.txt

for /f “tokens=* delims= ” %%a in (unduped.txt) do (
find “%%a” < unreachable.txt > nul
if errorlevel 1 echo %%a >> checklist.txt

Okay.  Now to try to pick out the Windows machines, and make sure I have the proper permissions for the task:

for /f “tokens=* delims= ” %%s in (checklist.txt) do (
sc \\%%s query workstation | findstr /c:”unavailable”
if errorlevel 1 echo %%s >> win.txt

for /f “tokens=* delims= ” %%a in (checklist.txt) do (
find “%%a” < win.txt > nul
if errorlevel 1 echo %%a >> nonwin.txt

for /f “tokens=* delims= ” %%s in (win.txt) do (
sc \\%%s query “Workstation” | findstr /c:”Access is denied”
if errorlevel 1 echo %%s >> access.txt

for /f “tokens=* delims= ” %%a in (win.txt) do (
find “%%a” < access.txt > nul
if errorlevel 1 echo %%a >> accessdenied.txt

And, finally, to check for the actual service.  I needed to find a specific service name related to a version, and, for some reason, checking for fails was more reliable than checking for the existence of the service:

for /f “tokens=* delims= ” %%s in (access.txt) do (
sc \\%%s query “Your Super Fab Service version 8.2″ | findstr /c:”RUNNING”
if errorlevel 1 echo %%s >> notv82.txt

It was actually a fun project, and made a day when I was worried about a family member in the hospital go by a lot faster.  I also saved it in case I ever needed it again, but I haven’t.  Yet.

Leave a Comment

Filed under scripting

Leave a Reply

Your email address will not be published. Required fields are marked *