Solved

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

Posted on 2014-02-17
4
723 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 54

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 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