Computer Hope
Microsoft => Microsoft DOS => Topic started by: Panthers_Den on May 11, 2010, 08:32:15 AM
-
So in my batch file, I can capture the errorlevel (the error number), but the system is also outputting it's own error msg to the user, how do I capture that msg?
OS: Windows 2003 Server
Here is what I get when I run the batch file (I know what's causing it, I just want to log the msg I get back when it's run)
D:\>cm_build.bat
System error 1219 has occurred.
Multiple connections to a server or shared resource by the same user, using more
than one user name, are not allowed. Disconnect all previous connections to the
server or shared resource and try again..
Here is what I get in the Logfile:
------------------------------------------------
-- Batch Ran on Tue 05/11/2010 at 8:57:05.97 --
------------------------------------------------
Error: Tue 05/11/2010 at 8:57:06.12
Error Location: UnMapped
Error Msg: error during mapping, Error Number: 2
Here's a snippet of the code:
@echo off
set logLocation="d:\BuildLogs.txt"
set deletedFoldersList="deletedfolders.txt"
echo ------------------------------------------------ >> %logLocation%
echo -- Batch Ran on %date% at %time% -- >> %logLocation%
echo ------------------------------------------------ >> %logLocation%
for /f "tokens=2-3" %%i in ('net use ^| find /i "Z:"') do (
IF %ERRORLEVEL% EQU 0 (
set local=%%i
set remote=%%j
goto :successfulRun
) ELSE (
goto :UnMapped
)
)
:UnMapped
echo unmapped >> %logLocation%
net use Z: \\a_drive\a_directory /USER:username password
IF %ERRORLEVEL% NEQ 0 (
set errorLoc=UnMapped
set errorMsg=error during mapping, Error Number: %ERRORLEVEL%
goto :errorFound
)
goto :successfulRun
:successfulRun
echo -- Batch run completed successfully on %date% at %time% -- >> %logLocation%
goto :eof
:errorFound
echo Error: %date% at %time% >> %logLocation%
echo Error Location: %errorLoc% >> %logLocation%
echo Error Msg: %errorMsg% >> %logLocation%
goto :eof
So I'm curious if there's a way to put that system msg from the dos prompt:
System error 1219 has occurred.
Multiple connections to a server or shared resource by the same user, using more
than one user name, are not allowed. Disconnect all previous connections to the
server or shared resource and try again..
into the log files? Being able to do caputer the dos system messages will go a long way in helping me to debug this batch file when it's run from scheduled tasks while the user is logged off.
Thx!
-
yes, it is more than possible, it is easy!
the redirection symbol takes screen output and puts it into a file, eg
net use Z: \\a_drive\a_directory /USER:username password>logfile
the single > will blank out the current contents of the file, using >> will append it to the end.
The End
Well, not quite, sometimes you cannot redirect the output like this because it is sent via the stderr channel, there is a fix for this, quote the channel number
net use Z: \\a_drive\a_directory /USER:username password 2>logfile
I havent used the redirection of the error channel, so I am not sure if 2>>logfile would work! (if it doesnt, then use 2> to redirect to a temp file and append the temp file to your logfile)
this is a useful resource http://ss64.com/nt/syntax-redirection.html (http://ss64.com/nt/syntax-redirection.html)
-
yes, it is more than possible, it is easy!
the redirection symbol takes screen output and puts it into a file, eg
net use Z: \\a_drive\a_directory /USER:username password>logfile
the single > will blank out the current contents of the file, using >> will append it to the end.
The End
Well, not quite, sometimes you cannot redirect the output like this because it is sent via the stderr channel, there is a fix for this, quote the channel number
net use Z: \\a_drive\a_directory /USER:username password 2>logfile
I havent used the redirection of the error channel, so I am not sure if 2>>logfile would work! (if it doesnt, then use 2> to redirect to a temp file and append the temp file to your logfile)
this is a useful resource http://ss64.com/nt/syntax-redirection.html (http://ss64.com/nt/syntax-redirection.html)
Yea, I had tried
net use Z: \\a_drive\a_directory /USER:username password>>logfile
which didn't work. I didn't think about trying 2>> (actually didn't even know what 2> did until now), so I just did that one and it worked, even with >> :-)
net use Z: \\a_drive\a_directory /USER:username password 2>>logfile
Thank you! :-)
Glad it was an easy change. Thanks for the link too, that'll come in handy for sure.
-
Thank you for the confirmation of 2>> - I thought it might work but had had no reason to try it before.
That site has many very useful examples of batch usage
-
By default, batch sees > (or >>) as 1> (or 1>>). That is what you see on screen as normal messages, 2> or 2>> will output error messages.
-
By default, batch sees > (or >>) as 1> (or 1>>). That is what you see on screen as normal messages, 2> or 2>> will output error messages.
1 is the standard output stream.
2 is the standard error stream.
>,>> , <, and | can have a stream number prefixed to them. for example, if you use:
program.exe 2>&1 | program2.exe
then the standard error and standard output of program.exewill both be piped to program2 as it's standard input.
-
program.exe 2>&1 | program2.exe
Why the &1, what does that do?
-
Why the &1, what does that do?
For the answer to that and any other questions about redirection, check out this article (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true)
8)
-
For the answer to that and any other questions about redirection, check out this article (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true)
8)
oh that's a helpful article, thanks :)