[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-02-17
4
Medium Priority
?
761 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 85

Accepted Solution

by:
oBdA earned 1000 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 57

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The viewer will learn how to dynamically set the form action using jQuery.

649 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