SLPowers
asked on
batch file that pulls variables from a txt file
I have a list of a few hundred computers.
I have a command I would like to run on each one.
I have tried psexec but it just will not work and takes hours to give me a output file of errors.
Forgive me I know BAT files a little but not VB.
I would much prefer a bat file if someone has one or can point me in the correct location.
The command is
REG QUERY \\remotepcname\HKLM\SOFTWA RE\somethi ng\FolderR edirect /v installed >>reginfo.txt
This will tell me the value of a key I need to check on for each pc in the list.
I would like to run this command for every pc listed in a text file I will call pclist.txt.
In the above example the pc name is \\remotepcname.
The text file would be a plain list of pc names in a column
Pc1
Pc2
Pc3
Pc4
And so on.
Thanks in advance.
I have a command I would like to run on each one.
I have tried psexec but it just will not work and takes hours to give me a output file of errors.
Forgive me I know BAT files a little but not VB.
I would much prefer a bat file if someone has one or can point me in the correct location.
The command is
REG QUERY \\remotepcname\HKLM\SOFTWA
This will tell me the value of a key I need to check on for each pc in the list.
I would like to run this command for every pc listed in a text file I will call pclist.txt.
In the above example the pc name is \\remotepcname.
The text file would be a plain list of pc names in a column
Pc1
Pc2
Pc3
Pc4
And so on.
Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A minor correction when I explained AmazingTech's batch file where I said:
"Dependent on whether this string is found (errorlevel = 0) or not found (errorlevel = 1), then the batch file just echoes a line to a text file that clearly states whether that string was found or not found."
What it actually does is send the names of the computers (ie. the contents of %%a in each pass) found to be online and offline to separate and logically named text files that clearly infer the status at that time.
"Dependent on whether this string is found (errorlevel = 0) or not found (errorlevel = 1), then the batch file just echoes a line to a text file that clearly states whether that string was found or not found."
What it actually does is send the names of the computers (ie. the contents of %%a in each pass) found to be online and offline to separate and logically named text files that clearly infer the status at that time.
I assume that you modified AmazingTech's code given in his first comment to contain your own text file of computer names?
ie. change computerlist.txt in:
for /f %%a in (computerlist.txt) do REG QUERY \\%%a\HKLM\SOFTWARE\someth ing\Folder Redirect /v installed >>reginfo.txt
to pclist.txt like this:
for /f %%a in (pclist.txt) do REG QUERY \\%%a\HKLM\SOFTWARE\someth ing\Folder Redirect /v installed >>reginfo.txt
AND that you also replaced the "\something\" registry key with a real one.
ie. change computerlist.txt in:
for /f %%a in (computerlist.txt) do REG QUERY \\%%a\HKLM\SOFTWARE\someth
to pclist.txt like this:
for /f %%a in (pclist.txt) do REG QUERY \\%%a\HKLM\SOFTWARE\someth
AND that you also replaced the "\something\" registry key with a real one.
ASKER
First excellent amazingtech thanks for the code and i had to give some points to billdl as that really was extremely nice of him to break it down for me like that. Thanks so much for all your help. It is very appreciated.
Thank you SLPowers. You're very welcome. I have always managed to place myself again in the position of looking at such things and scratching my head, puzzled at what the code actually did. I hope AmazingTech approves of my explanation of his batch file and that it has saved him from some typing ;-)
Thank you BillDL for providing the explaination.
Just one note:
I typically will always use if errorlevel 1 (error or not found) and if not errorlevel 1 (no errors found).
It is difficult to sometime exactly know what the variable %errorlevel% is set to.
Some programs will set the errorlevel variable to different specific values like 5 for access denied. But using if errorlevel 1 will be set when there is any error. Never use if errorlevel 0 this never changes (or atleast I've never found how it changes.
An example of what I mean :
@echo off
pingin -n 127.0.0.0
if "%errorlevel%"=="0" start /b /wait notepad.exe
if "%errorlevel%"=="1" start /b /wait calc.exe
echo %errorlevel%
Since pingin is not a valid command neither notepad.exe or calc.exe will start. Since %errorlevel% is set to 9009.
This will log success and errors:
@echo off
pingin -n 127.0.0.0
if not errorlevel 1 echo No error occurred>>Status.log
if errorlevel 1 echo Error #%errorlevel% occured>>Status.log
echo %errorlevel%
Just one note:
I typically will always use if errorlevel 1 (error or not found) and if not errorlevel 1 (no errors found).
It is difficult to sometime exactly know what the variable %errorlevel% is set to.
Some programs will set the errorlevel variable to different specific values like 5 for access denied. But using if errorlevel 1 will be set when there is any error. Never use if errorlevel 0 this never changes (or atleast I've never found how it changes.
An example of what I mean :
@echo off
pingin -n 127.0.0.0
if "%errorlevel%"=="0" start /b /wait notepad.exe
if "%errorlevel%"=="1" start /b /wait calc.exe
echo %errorlevel%
Since pingin is not a valid command neither notepad.exe or calc.exe will start. Since %errorlevel% is set to 9009.
This will log success and errors:
@echo off
pingin -n 127.0.0.0
if not errorlevel 1 echo No error occurred>>Status.log
if errorlevel 1 echo Error #%errorlevel% occured>>Status.log
echo %errorlevel%
Hi SLPowers.
You raised some very good points there AmazingTech.
Yes, an errorlevel NOT EQUAL TO Zero is usually assumed to be a failure, and it's always best to use:
IF NOT ERRORLEVEL 0
UNLESS you are testing for one or more known return codes for a very specific reason, for example:
The 5 return codes for XCOPY (http://commandwindows.com/xcopy.htm), third-party programs that have return codes right up to 255, etc, etc.
This is a good discussion, particularly the advice about testing from the highest number first:
http://www.robvanderwoude.com/errorlevel.php
Actually, I need to correct errors in my small example batch file anyway. I referred to LocalHost just above it as 127.0.0.1 and then for some reason used 127.0.0.0 in the batch file. The correct IP Address of LocalHost is 127.0.0.1.
I also used PING with the -n switch (number of echo requests to send), but somehow ended up missing out the number. The correct usage of PING with the -n switch in my example should have been (3 times):
ping -n 3 127.0.0.1 | find /i "reply from" > nul
Duh!! That's what happens when things are typed off the top of my head without testing.
I have added the redirection to NUL ( > nul) to the command above to send the 3 replies into a void rather than output them to screen. AmazingTech introduced the method for redirecting activities to a log file above. The following two pages are very useful and show you how to pick and choose what you want to redirect to file, eg. only errors:
http://www.robvanderwoude.com/battech_redirection.php
http://www.robvanderwoude.com/redirection.php
Used in context only with the FIND command, and referring to my previous example, here is another method of using the ErrorLevel variable in a test. Just swap the leading REM around between the top lines to test it. REM remarks out the line it precedes so it is ignored.
I'm only providing this as an example where results can be reasonably anticipated and may serve a useful purpose, it's really just an example of how you can add variables to make up a text string, BUT you should note what AmazingTech has highlighted above about sticking to IF NOT ERRORLEVEL 0, because it is important and useful to know. Maybe the concept will be useful to you in some other context.
Regards
Bill
You raised some very good points there AmazingTech.
Yes, an errorlevel NOT EQUAL TO Zero is usually assumed to be a failure, and it's always best to use:
IF NOT ERRORLEVEL 0
UNLESS you are testing for one or more known return codes for a very specific reason, for example:
The 5 return codes for XCOPY (http://commandwindows.com/xcopy.htm), third-party programs that have return codes right up to 255, etc, etc.
This is a good discussion, particularly the advice about testing from the highest number first:
http://www.robvanderwoude.com/errorlevel.php
Actually, I need to correct errors in my small example batch file anyway. I referred to LocalHost just above it as 127.0.0.1 and then for some reason used 127.0.0.0 in the batch file. The correct IP Address of LocalHost is 127.0.0.1.
I also used PING with the -n switch (number of echo requests to send), but somehow ended up missing out the number. The correct usage of PING with the -n switch in my example should have been (3 times):
ping -n 3 127.0.0.1 | find /i "reply from" > nul
Duh!! That's what happens when things are typed off the top of my head without testing.
I have added the redirection to NUL ( > nul) to the command above to send the 3 replies into a void rather than output them to screen. AmazingTech introduced the method for redirecting activities to a log file above. The following two pages are very useful and show you how to pick and choose what you want to redirect to file, eg. only errors:
http://www.robvanderwoude.com/battech_redirection.php
http://www.robvanderwoude.com/redirection.php
Used in context only with the FIND command, and referring to my previous example, here is another method of using the ErrorLevel variable in a test. Just swap the leading REM around between the top lines to test it. REM remarks out the line it precedes so it is ignored.
I'm only providing this as an example where results can be reasonably anticipated and may serve a useful purpose, it's really just an example of how you can add variables to make up a text string, BUT you should note what AmazingTech has highlighted above about sticking to IF NOT ERRORLEVEL 0, because it is important and useful to know. Maybe the concept will be useful to you in some other context.
Regards
Bill
@echo off
ping -n 2 127.0.0.1 | find /i "Reply from" > nul
REM ping -n 2 127.0.0.1 | find /i "Some Bogus Text String" > nul
goto ReturnCode_%ERRORLEVEL%
:ReturnCode_0
echo.
echo The FIND command returned Code 0 meaning "Reply From" WAS found
echo Therefore the PING command reached the specified host.
echo.
pause
goto END
:ReturnCode_1
echo.
echo The FIND command returned Code 1 meaning "Reply From" NOT found
echo Therefore the PING command DID NOT reach the specified host.
echo.
pause
goto END
:END
EXIT
Whoops, did it again. I meant to emphasise AmazingTech's advice about NOT using Errorlevel Zero and emphasised the opposite.
ASKER
Wow thanks guys.
ASKER
Thanks
for /f %%a in (computerlist.txt) do ping %%a