Thursday, February 23, 2012
 
 

Print log in Windows XP

Monitoring print job is sometime very important. Organization (or may be individuals) may need to monitor print job probably to reduce waste or misusage of print. There could be other reason as well. In any case, it is helpful to know how to monitor your printer. One of the way which I know works well in Windows XP (or probably older version as well).

To monitor print job, one could either use print notification event (which I am not sure how to attach) or use the event viewer utility logs in Windows XP.  Log from event viewer utility can be supplied to your program which can parse it show it in whatever format you need. 

However, Windows XP, by default, does not automatically enable the log of print job (like type of document, timestamp, number of pages etc). To enable it,

1) please click on “Start”  –>  “Printers and Faxes”.

2) In “Printers and Faxes” explorer, click on “File”  –> “Server Properties”.

3) In "Print Server Properties" configuration dialog, click on “Advanced” tab and make sure that  “Log spooler information events” checkbox is checked.

Configure Print Server

When enabled, print log can either be viewed using Event viewer utility (Control Panel –> Administrative Tools –> Event Viewer –> System) or execute command using eventquery. For example,

cscript c:\WINDOWS\system32\eventquery.vbs /V /FO CSV /L System /FI "source eq print"

would give result as,

Result of Print EventQuery

If you look carefully, you would notice that in “Description” field data, required details are available. For example- Document serial number, document name, User name which executed print, printer name, number of pages printed etc. 

Further, You can also provide other option with eventquery to filter/format in output. (Obviously, you can find other possible option using “cscript c:\WINDOWS\system32\eventquery.vbs /?” ).

 

Menu based Environment Selection Script

During development, you will often see yourself playing with different environment (in unix based system). For example, at one instance, you will need Oracle DB environment so that you can use sql prompt. In other case, you might need your application environment to work with it. Or you might just need default environment.

It is always painful to set proper environment after logging in to the machine as you will need to remember which environment script to source (probably alias can help here!). I always had this problem until I implemented environment selection script.

Basically the idea was to

1) Create scripts which can source required environment variable and put all the scripts at one location.

2) Prepare another script which would execute at login. Lets call it "ShellEnv".

3)  “ShellEnv” should read all the environment specific scripts from the predefined location and show a console based menu.

4) Based on user selection, “ShellEnv” would source the corresponding script and hence your environment would have correct configuration.

So the script would look like 

#! /bin/ksh
export ENV_FILE_LOC=/home/kunalkumar/env
prepareMenu
displayMenu
readValues
executeOptions

Now the problem is, how would “ShellEnv” determine which script is for which environment and how it should present the menu? Obviously, script should have certain indicator which can help in preparing menu. I choose to add “DISPLAY_MENU_STRING” in all my configuration files. For example, my Oracle DB scripts contains the following (Note that DISPLAY_MENU_STRING is commented in the script.)

#DISPLAY_MENU_STRING=Oracle 11.2g (kspace)
export ORACLE_HOME=/usr/oracle/11.2
export ORACLE_SID=test

With this, “ShellEnv” can determine how the menu should be constructed to display. Using “DISPLAY_MENU_STRING”, function prepareMenu

prepareMenu(){
        unset fileName
        unset menuString
        set -a fileName
        set -a menuString
        index=0;
        for file in `ls $ENV_FILE_LOC`
        do
                string=`grep "DISPLAY_MENU_STRING" $ENV_FILE_LOC/$file | awk -F"=" '{ print $2 }'`
                if [[ $string != "" ]]; then
                        fileName[index]=$file;
                        menuString[index]=$string;
                        index=`expr $index + 1 `;
                fi
        done
}

Basically, It prepared array of file names and corresponding display string while looking into the predefined directory. After preparing the list, next task was to display the menu and hence

displayMenu(){
        echo "Available Shell Configuration. ";
        index=1;
        totalMenuItem=${#fileName[*]};
        while [[ $index < $totalMenuItem ]]; do
                echo "$index  - ${menuString[$index]}";
                index=`expr $index + 1`
        done;
        echo "Please select configuration : ";
}

After displaying the menu, user could choose the environment by providing input. The input would be consumed by readValues and used by executeOptions. Function executeOptions would source the environment as selected by the user. I also included a script to source default configuration which should be available irrespective of any environment selected. and hence the function

executeOptions(){
        OLDIFS=$IFS;
        export IFS=" ";
        inputArray=$inputOptions;
        totalInputOptions=${#inputArray[*]};
        echo "Sourcing Default Environment...";
        . $ENV_FILE_LOC/1_Default
        if [[ $totalInputOptions -eq 0 ]]; then
                echo "No option selected!";
                return;
        fi
 
        index=0;
        while [[ $index < $totalInputOptions ]]; do
                echo "Sourcing environment from ${fileName[ ${inputArray[$index]} ]}";
                . $ENV_FILE_LOC/${fileName[${inputArray[$index]}]};
                index=`expr $index + 1`;
        done
        export IFS=$OLDIFS
}

I included this script in .profile so that it gets executed at login and I can choose my environment by typing the option.

login: kunal
password:
Available Shell Configuration. 
1 – Oracle 11.2g (kspace)
2 – My App Environment
3 – Test Environment
Please select configuration : 1
Sourcing Default Environment...
Sourcing environment from Oracle_11_2.ksh

If you are interested in this script, you can find it at github.

 
 
 

 

Find Me!
View Kunal Kumar's profile on LinkedIn