Standard IO Redirection and Event Handler Processes - Script Execution
Script Execution
If an event handler executes a script, standard I/O redirection can be done within the script itself. This method provides a little more flexibility with respect to assigning unique names to files that store captured output. If unique names are needed, logic can be set up within the script to construct the name and to ensure that the file does not already exist.
The following figures illustrate sample scripts that contain logic for generating unique file names.
Bourne Shell Script
UNIX
This sample is a Bourne shell script that uses a combination of environment variables set by the UEM Server, the script's process ID, and the current system date/time in order to construct a unique file name used to store the output of the ls -al command.
#!/bin/sh rc=0 ## Set a date string in the format yyyy.mm.dd. dt=`date +%Y.%m.%d` ## Set a time string in the format hh.mm.ss tm=`date +%H.%M.%S` ## Save the process ID pid=$$ ## Remove the extension from the file tracked by UEM. uemFName=`basename "$UEMORIGFILE" | sed 's/(^.*).(.*)/1/'` ## Construct a filename using the name of the file tracked by UEM, the ## current date, the current time, and the process ID. fname=$uemFName.$dt.$tm.$pid.txt ## Execute the command. Redirect the output to the file name ## constructed above. ls -al >$fname ## Set the return code and exit the script rc=$? exit $rc
Windows Batch File
Windows
This sample is a Windows batch file that uses a combination of environment variables set by the UEM Server and the current system date/time in an attempt to construct a unique file name used to store the output of the dir command. Because multiple instances of this script can be executing at the same time, and a process ID is not available to a Windows batch file, a loop exists to retry the file name construction if a matching file is found.
@echo off :: Main script flow set rc=0 call :SetVariables call :SetupOutputFile :: Now, execute the command. Redirect the contents of this directory :: and all subdirectories to the file identified by %fname%. dir /o/-p/s >%fname% set rc=%ERRORLEVEL% :: Exit the script goto Exit :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Subroutines :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :SetVariables :: Set a date string in the format yyyy.mm.dd. for /f "tokens=2,3,4 delims=/ " %%a in ('date /t') do set dt=%%c.%%a.%%b :: Set a time string in the format hh.mm.ss for /f "tokens=1,2 delims=: " %%a in ('time /t') do set tm=%%a.%%b :: Remove file extension for /f "tokens=1,2 delims=." %%a in ("%UEMORIGFILE%") do set uemFName=%%a :: Set the file name set fname=%uemFName%.%dt%.%tm%.txt :SetVariablesExit goto :EOF :: :: Test for the existence of the output file. If the file is found, loop :: for a while then reset the time variable and check again. :: :SetupOutputFile :: If the file doesn't exist, create it to prevent another instance of this :: script from grabbing it. if not exist %fname% ( echo. >%fname% goto SetupOutputFileExit ) :: Otherwise, wait for a count of 10,000 (approx 1 sec) and try again. if exist %fname% for /l %%I in (1,1,10000) do echo. >NUL call :SetVariables goto SetupOutputFile :SetupOutputFileExit goto :EOF :Exit exit %rc%
Note
In both of these examples, the redirection is done at the point at which the process is invoked. If the script itself also generates output that needs to be captured, this file would need to be provided via the event definition's handler options (see Standard IO Redirection and Event Handler Processes - Handler Options.