GRitchie Posted December 9, 2015 Posted December 9, 2015 Hello, I've written an imaging script (installs software, sets up computer etc.) and I want to log everything out to a text file (C:\STAFF-IMAGE-LOG.log). How do I get it to output EVERYTHING? I've tried using set log="C:\file.log" and then >> %log% The issue is, when you add this to a line with @echo at the start, it prevents it from echoing to the CMD window. Ideas?
X-13 Posted December 9, 2015 Posted December 9, 2015 There's a dirty way of doing it... Output everything to the log file, then call the log file to console. So, for example: ping localhost > log.log copy log.log con There's a delay on the information, but it works. [To a certain degree... I know there's ways to break it.]
GRitchie Posted December 9, 2015 Author Posted December 9, 2015 Hm, but would that log results of commands and things? Where it's wanting user input The script has sections where it records variables and does different things according to those etc.
X-13 Posted December 9, 2015 Posted December 9, 2015 Hm, but would that log results of commands and things? Yup, you're redirecting the output of the command to a file. Where it's wanting user input The script has sections where it records variables and does different things according to those etc. ...that's one of the ways to break it. You can't input anything, because it's output to a logfile. If you can get your head 'round this [which I'm struggling to], it could be useful. But it'd be a mess of code.
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 Oh. Bugger. Is there any other ways? Haha!
X-13 Posted December 10, 2015 Posted December 10, 2015 Oh. Bugger. Is there any other ways? Haha! It depends, what exactly do you want/need it to do?
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 I just need it to output everything it does so that the one day it errors I can look through the log to see why. It echoes things to the user, it asks for user input (me/technicians) for example 'Laptop/Desktop?' and then it will or won't run 'netsh add profile' But if I just output the actual commands that do something it just says 'Command completed successfully' which obviously is neither use nor ornament...! If I then >> %LOG% the @echo lines, it doesn't then display them in the CMD window, which is stupid! I can't really create two versions of every echo just to show in the log as well, as my script would be a right mess...
X-13 Posted December 10, 2015 Posted December 10, 2015 I just need it to output everything it does so that the one day it errors I can look through the log to see why. It echoes things to the user, it asks for user input (me/technicians) for example 'Laptop/Desktop?' and then it will or won't run 'netsh add profile' But if I just output the actual commands that do something it just says 'Command completed successfully' which obviously is neither use nor ornament...! If I then >> %LOG% the @echo lines, it doesn't then display them in the CMD window, which is stupid! I can't really create two versions of every echo just to show in the log as well, as my script would be a right mess... Leave the echo and input requests visible, output the commands. For the things that just output "command completed", redirect a custom echo saying what it is as well. From the sounds of it, you don't need the echos in the logfile any way.
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 For the things that just output "command completed", redirect a custom echo saying what it is as well. How do I do that? Without writing another line saying (for eg): @ ECHO Installing NET Framework >> %LOG%
Steve21 Posted December 10, 2015 Posted December 10, 2015 (edited) Unless you need to it show on screen there's a much easier way. Just use 2 BAT files and you can steal the output from the entire bat file. Example: BAT1: @echo off set /p Var1=ENTER STUFF NOW MORTAL: test2 %Var1% > test123.txt echo. pause BAT2: @echo off echo %1 ping %1 Output: ITS-001 Pinging its-001.DOM.sch.uk [fe80::d97b:ac89:51d3:99ff%3] with 32 bytes of data: Reply from fe80::d97b:ac89:51d3:99ff%3: time<1ms Reply from fe80::d97b:ac89:51d3:99ff%3: time<1ms Reply from fe80::d97b:ac89:51d3:99ff%3: time<1ms Reply from fe80::d97b:ac89:51d3:99ff%3: time<1ms Ping statistics for fe80::d97b:ac89:51d3:99ff%3: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms Would that work or did you need it show output on console? Steve Edited December 10, 2015 by Steve21
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 I need it to output to the console as well unfortunately
Steve21 Posted December 10, 2015 Posted December 10, 2015 I need it to output to the console as well unfortunately Only way I've seen to do that for "all output" is by doing the outputs twice. e.g. Save command output to variable, then write once to log and once to console. Obviously anything with a direct > or >> redirects the entire stream so you wouldn't see it. Problem is depending on the outcome of each command/program you'd need a different process to read it into a variable. Depending on how big the file etc is :s What's your script like size wise? Steve
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 (edited) The batch script in file size is 5KB (it does call other scripts however). The number of lines is 158 (currently) EDIT: The scripts called total to 26KB Edited December 10, 2015 by GRitchie
Steve21 Posted December 10, 2015 Posted December 10, 2015 The batch script in file size is 5KB (it does call other scripts however). The number of lines is 158 (currently) Not so many lines then, one way to do it but it's not exactly pretty is doing temporary files and re-reading them. Example: echo blah > test.txt set /p Var= And then effectively you got a log per command. But you could merge them all at the end Steve
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 But when I do @ Echo text > output.log won't that stop it from outputting to the console?
Steve21 Posted December 10, 2015 Posted December 10, 2015 But when I do @ Echo text > output.log won't that stop it from outputting to the console? Yes but then you're reading it back in from the file - set /p Var= Example echo BLAH > test.txt s /p Var= echo %Var% BLAH etc if that makes sense? Steve
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 Oh I see, but for what that's worth I may as well have two lines of every echo. One with >> %LOG% and one without? Haha!
Steve21 Posted December 10, 2015 Posted December 10, 2015 Oh I see, but for what that's worth I may as well have two lines of every echo. One with >> %LOG% and one without? Haha! Yes but then you'd run each command twice. So if you're running for example Install.NET you'd run it once and it'll work (example) and log that, then re-run it and it'd fail and show that on console Steve
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 Oh, I see... Yeah... Hm, why doesn't it just have a function to write everything to a log... Just seems like such a simple feature?
Steve21 Posted December 10, 2015 Posted December 10, 2015 Oh, I see... Yeah... Hm, why doesn't it just have a function to write everything to a log... Just seems like such a simple feature? There is, but not at the same time as the Output don't think any language has that really. Either you're putting the output to X or Y Not really sure the overall purpose of both either, as if you're sitting at the computer while it's running to see the console then you don't need a log, and if you're logging it without being there you don't need the console? Steve
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 Touché, but what about when you're watching it and it errors, but then closes quickly. You've seen it error, you know when, but you don't know why...
Steve21 Posted December 10, 2015 Posted December 10, 2015 Any reason you don't use Error codes etc or the magic "Pause" Steve
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 It's not worth the time for error codes haha! And if I were to put a pause in, I'd need a 'IF ERROR' on every line wouldn't I? Or can I sit the entire thing in an if error loop?
Steve21 Posted December 10, 2015 Posted December 10, 2015 The bit I don't get, and maybe I'm still missing the obvious here. Is if you just want to see when it errors and what caused it, while can't you read the log file still and just not output anything to the console? As if it's closing instantly at the end in your script currently you wouldn't see any errors on screen anyway? But you should be able to use if not ERRORLEVEL 0 pause but then what's the point of the log file? Steve
GRitchie Posted December 10, 2015 Author Posted December 10, 2015 Shall I PM you the script and see what you make of it? Might be able to understand what I'm meaning at times!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now