Solved

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

Posted on 2014-02-17
4
678 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 83

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 51

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now