Solved

How do I append running result logs to text file in a batch

Posted on 2014-02-17
4
703 Views
Last Modified: 2014-02-25
I use the batch below. with the command pspasswd I am changing the local administrator password of the several machines which listed in "MachineSource.txt".
To Access to the machine admin user is "authorized_user" and password is "authorized_user_pass".
I want to have Error log. But the end part of the command, you se >2 d:\Errorlog.txt" saving only the last transaction's log.
How do I append every transaction's or only error transactions's log that pspasswd command does.


The Batch is:

@echo off
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I

goto :EOF

:begin
set srv=%1
:get info

pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 2> "D:\ERRORLOG.TXT"
0
Comment
Question by:certuran
4 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 39864932
You need to use ">>" to not overwrite an existing file.
Only Errorlog to file:
@echo off
set ErrorLog=D:\ERRORLOG.TXT
if exist "%ErrorLog%" del "%ErrorLog%"
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I
goto :EOF

:begin
set srv=%1
:get info
pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 2>>"%ErrorLog%"

Open in new window

Standardlog and Errorlog separately:
@echo off
set StandardLog=D:\STANDARDLOG.TXT
set ErrorLog=D:\ERRORLOG.TXT
if exist "%StandardLog%" del "%StandardLog%"
if exist "%ErrorLog%" del "%ErrorLog%"
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I
goto :EOF

:begin
set srv=%1
:get info
pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 1>>"%StandardLog%" 2>>"%ErrorLog%"

Open in new window

And combined in one file:
@echo off
set CombinedLog=D:\COMBINEDLOG.TXT
if exist "%CombinedLog%" del "%CombinedLog%"
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I
goto :EOF

:begin
set srv=%1
:get info
pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 1>>"%CombinedLog%" 2>&1

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39865162
I often do these slightly differently so wanted to share a different style.  I wrap the chunk of code that I want to capture output from in a containing set of parens, and then after the last paren redirect the STDOUT and STDERR streams as desired (showing them merged below). This makes the code a little easier to maintain I find, and removes the need for specifying the output redirection on each command when there are multiples in the block of code.

This approach also saves having to DEL the output file manually if you want to start it fresh each execution.

I also wanted to show a technique where you can place the program execution right in the FOR loop, saves code and will run slightly faster.

@echo off

set MachineFile=MachineSource.txt
set LogFile=D:\ERRORLOG.TXT

(
  for /F "usebackq delims==" %%I in ("%MachineFile%") do (
    pspasswd \\%%I -u auhorized_user -p authorized_user_pass administrator abc123
  )
) >"%LogFile%" 2>&1

Open in new window

~bp
0
 

Expert Comment

by:SmartRxCloud
ID: 39865307
@echo off
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I

goto :EOF

:begin
set srv=%1
:get info

pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 2[b]>> [/b]"D:\ERRORLOG.TXT"

Open in new window



more info refer this link


http://www.robvanderwoude.com/redirection.php
0
 

Author Closing Comment

by:certuran
ID: 39885194
It worked proper to just what I wanted to do. And much, you have given alternatives those all usefull. Thank you very much.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question