Email notifications without BLAT
- 10 years ago
However, sometimes it is useful to capture the output, and errors if any, of commands embedded within scripts that NetBackup calls. So, I usually like to start Windows DOS batch scripts with this at the very start:
@echo off setlocal enabledelayedexpansion set z_path=%~dp0 set z_name=%~n0 set z_file_log=!z_path!!z_name!.log
...which means:
1) @echo off - the leading @ stops the next command from being echo'd, and echo off disables echoing for the rest of the script
2) setlocal enabledelayedexpansion - means that variables can be expanded within loops using the exclamation (or shriek) characters, i.e. these ! !, because if you try to use a % % around variable names inside code inside a loop then they pre-expanded before the loop actually starts, whereas using ! ! means that variables get expanded at the time the line of code being intrepreted actually executes.
3) set z_path=%~dp0 - means, store in the variable named z_path the value of parameter 0 (i.e. the full file specification of the script being executed), but extract just the drive letter and folder path (dp = drive+path) segment from the full path of the file name of the script being executed.
4) set z_name=%~n0 - means, store in the variable named z_name the value of paramter 0 (i.e. the full path specification of the script being executed), but just extract the name part/segment of the full script file name.
5) set z_file_log=!z_path!!z_name!.log - means store in a variable named z_file_log, the contents of the variables z_path and z_name and add the characters .log to the end of those values - so, what this means is that the variable z_file_log now holds a full drive letter + folder path + filename + file extenstion of '.log', which is great because it means that now we can use that full "log file" specification file name to capture output from other commands - which also means that the log file for any script that you use this code in also lives in the same folder as the script that NetBackup calls.
.
Now that we have a decent log file name, this means that instead of binning (to nul, to the bit-bucket) any output or errors from a call to 'cscript', that instead now we can capture the output, so in your nbmail.cmd script you could now do something like:
cscript mailer.vbs %1 %2 %3 >>"!z_file_log!" 2>&1
...which means:
- cscript - means run the VBscript interpretation engine named cscript.exe
- mailer.vbs - means tells the 'cscript' engine to open and begin reading and to start executing the contents of mailer.vbs
- %1 %2 %3 - means pass the three DOS parameters (that mail.cmd received from NetBackup) to the command line so the cscript will make them available to mailer.vbs
- >>"!z_file_log!" - means >> = append, and append to a file whose name is inside z_file_log, i.e. the use of surrounding ! ! characters tell DOS to expand the contents of the variable named z_file_log, and the surrounding quotes " " are required because the full drive+folder+name+.log inside z_file_log may contain spaces, and so using " " tells DOS to keep it all together
- 2>&1 - means do the same for stderr as you do for stdout, and so any DOS or cscript errors also get appended to the contents of the log file.
- and note how we didn't need a leading '@' on this command, because we already disabled echoing above with the '@echo off' command.
.
And I like to capture the exit status of the cscript command, so that I can writethe status to the log file, so that we can see why our cscript command, or the execution of mailer.vbs, may have failed, e.g.:
set z_sts=!errorlevel! echo call to cscript status code was !z_sts! >>"z_file_log!" 2>&1 exit /b !z_sts!
...which means:
- set z_sts=!errorlevel! - means store in teh variable named z_sts the volume of the variable errorlevel, see how the use of !errorlevel! tells DOS to expand errorlevel in to the contents of errorlevel.
- and then the echo sends a string of words and the contents (because we're using ! ! again) of z_sts to stdout, but our use of >>"!z_file_log!" means append stdout to the end of the file whose name is expanded out of the contents of variable z_file_log, and finnaly our old friend 2>&1 means do the same with errors which be generated by the echo command.
- exit /b !z_sts! - means exit teh script, and /b means just exit this level instance ie. don't exit the whole chain of calling/called DOS scripts, and !z_sts! tells the exit command to pass back the contents of the variable z_sts, i.e. to pass backup a final exit code back to the NetBackup process which called nbmail.cmd in the first place.
.
So, maybe your nbmail.cmd could look someting like:
@echo off setlocal enabledelayedexpansion set z_path=%~dp0 set z_name=%~n0 set z_file_log=!z_path!!z_name!.log cscript mailer.vbs %1 %2 %3 >>"!z_file_log!" 2>&1 set z_sts=!errorlevel! echo call to cscript status code was !z_sts! >>"z_file_log!" 2>&1 exit /b !z_sts!
...which will now run silently, and pass back a proper exit code, and capture all output and all errors in a file named "nbmail.log", as a companion file in the same folder that "nbmail.cmd" lives in.
HTH.