Sign in to follow this  
Followers 0
bsaoudi

PrintJobManager

2 posts in this topic

#1 ·  Posted (edited)

print quota for users

http://bsaoudi.free.fr/PrintJobManager/

#cs -------------------------------------------

 AutoIt Version: 3.3.6.1
 Author: Bertrand.Saoudi @ univ-littoral.fr

   Managing printer queues according to a quota system.
     - To run on the print server! (PC & printer )
     - The treatment of printers and queues is through a WMI object
        (Windows Management Instrumentation)
     - Quotas are stored in a database SQLITE
     - A custom message is broadcast to the user through Active Desktop (thank you Ludo)
     - Management of the base will shell with real SQL (sqlite3.exe printers.db)
        > UPDATE accounts SET quota = 0 WHERE name = 'test'; (it is no longer right)
        > SELECT * FROM accounts ORDER BY used DESC; (to see the largest consumer)
          complicating a bit (join), you can do it by Printer ...

   Documents:
     - AutoIt: http://www.autoitscript.com/autoit3/docs/
     - Sqlite: http://www.sqlite.org/docs.html
     - Win32_Printers: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394363 (v = VS.85). Aspx
     - Win32_PrintJob: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394370 (v = VS.85). Aspx

   AutoIt forum: http://www.autoitscript.com/forum/
   AutoIt examples: http://www.autoitscript.com/forum/files/

  License:
     I wrote this program on a Sunday, this is not a job, it is forbidden to sell it.
     All lines are commented out, this is done to put the hands in! ^^
      ("get your ass in gear!") original french expression is more meaningful ("get out fingers from your ass")
     It'll be nice to send me an email if you are using and let me know the changes.

#ce ------------------------------------------------


;******************************
;******* includes ********
;******************************
; to use SQLITE with AutoIt
#include 
#include 


;******************************
;******** vars **********
;******************************
; default quota
#cs ---------------------------------------------------
it can easily be reset automatically
 on a comparison of date, or other ...
#ce ---------------------------------------------------
$quota = 500
; Active Desktop Wallpaper
#cs ---------------------------------------------------
 Set the wallpaper of the user GPO
 User /
   Administration model /
     Active Desktop /
      Wallpaper
   Myserver  ActiveDesktop % username%. Htm
(Disable the classic interface required)
print.htm is in the same folder
{C}
#ce ---------------------------------------------------
$HTML = FileRead('print.htm')
; share folder ('my_server'ActiveDesktop) of wallpapers
#cs ---------------------------------------------------
you can use 'FileSelectFolder'
and 'net share' (dos) or 'Win32_Share' (WMI)
#ce ---------------------------------------------------
$background_dir = "C:PARTAGESActiveDesktop"

; variables necessary for SQLITE
Local $hQuery, $aRowUser, $aRowPrinter


;******************************
;******* Initialization *******
;******************************
; The WMI object coveted !!
$objWMIService = ObjGet("winmgmts:" & @ComputerName & "rootcimv2")
; DataBase
_StartSQLITE()
; printers
_StartPrinters($objWMIService)
; 'Alt+p' to stop it
HotKeySet("!p","_Stop")



;*******************************
;*******************************
;****** LE PROGRAM !!! ******
;*******************************
;*******************************
while 1
; we're working with print jobs, Win32_PrintJob !!
$colInstalledPrinters = $objWMIService.ExecQuery("SELECT * FROM Win32_PrintJob", "WQL", 0x30)
; for each
For $objPrinter in $colInstalledPrinters
; debug : displays status ( uncomment to view)
;ConsoleWrite($objPrinter.StatusMask & @lf )
; pause work happens ( 8 = spooling, 16 = printing )
If $objPrinter.StatusMask = 8 Or $objPrinter.StatusMask = 24 Then $objPrinter.Pause
; processing a job completely received ( 1 = pause )
If $objPrinter.StatusMask = 1 Or $objPrinter.StatusMask = 17 Then _Process($objPrinter)
Next
; small pause (avoids overloading the processor)
Sleep(10)
WEnd



;*****************************
;****** functions ********
;*****************************


;**********************************************************
; Returns the quota remaining of owner
; $job object is Win32_PrintJob type
;**********************************************************
Func _DrawingRight($job)
; we need quota and pages used
_SQLite_Query(-1, "SELECT quota, used FROM accounts WHERE name = '" & $job.Owner & "';", $hQuery)
; if user has not yet printed
If _SQLite_FetchData($hQuery, $aRowUser, False, False) <> $SQLITE_OK Then
; we put him in the database with his quota
_SQLite_Exec(-1, "INSERT INTO accounts (name,quota, used) VALUES ('" & $job.Owner & "'," & $quota & ",0);")
; simulate return of statement
Dim $aRowUser[2] = [500,0]
EndIf
; just to not have nasty Warning !!!
_SQLite_QueryFinalize ($hQuery)
; we need also cost per page of printer
_SQLite_Query(-1, "SELECT cost FROM printers WHERE name = '" & _PrinterName($job) & "';", $hQuery)
; "who is the sausage that renames a printer ?"
If _SQLite_FetchData($hQuery, $aRowPrinter, False, False) <> $SQLITE_OK Then MsgBox( 0,"error","printer not found")
; just to not have nasty Warning !!!
_SQLite_QueryFinalize ($hQuery)

; if printer doesn't cost, enjoy !!
If $aRowPrinter[0] = 0 Then Return $quota
; otherwise quota - used / cost ( color printer can be more expensive )
Return ( $aRowUser[0] - $aRowUser[1] ) / $aRowPrinter[0]
EndFunc


;**********************************************************
; processing job
;**********************************************************
Func _Process($job)
; quota consultation
If $job.TotalPages > _DrawingRight($job) Then
; delete if not enough pages
$job.Delete_
Else
; date formating
$date = @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
; store job ( apostrophe in the name of the application crashes )
_SQLite_Exec(-1, "INSERT INTO jobs (document,owner,host,printer, pages, date) VALUES ('" & StringReplace($job.Document,"'"," ") & "', '" & $job.Owner & "', '" & $job.HostPrintQueue & "', '" & $job.DriverName & "', '" & $job.TotalPages & "', '" & $date & "');")
; console display ( nothing if compiled )
ConsoleWrite($job.Owner & "; " &$job.Totalpages & "; " & _PrinterName($job) & "; " &$job.Document & "; " &$job.TotalPages & @lf)
; increment used counter
_SQLite_Exec(-1, "UPDATE accounts SET used = " & $aRowUser[1] + ( $job.TotalPages * $aRowPrinter[0] ) & " WHERE name = '" & $job.Owner & "';")
; PRINT
$job.Resume
; making wallpaper
_Background($job.Owner,$aRowUser[0] - $aRowUser[1] - ( $job.TotalPages * $aRowPrinter[0] ))
EndIf
EndFunc


;******************************************
; making custom wallpaper
;******************************************
Func _Background($username, $quota)
; delete older
FileDelete($background_dir & "" & $username & ".htm")
; writing new with quota instead of ***
FileWrite($background_dir & "" & $username & ".htm", StringReplace($HTML, "***", $quota) )
EndFunc


;******************************************
; the end, Alt+p
;******************************************
Func _Stop()
; we're working with printers, Win32_Printer !!
$colInstalledPrinters = $objWMIService.ExecQuery("Select * from Win32_Printer")
; for each
For $objPrinter in $colInstalledPrinters
; pause ( modifying script without letting job, we not laugh !)
$objPrinter.Pause()
Next
; closing 'clean'
_SQLite_Close($Db)
_SQLite_Shutdown()
;bye bye
Exit 0
EndFunc


;******************************************
; return printer's name
; from job's name
;******************************************
Func _PrinterName($job)
; splitting ! job's name = printer's name, job's number
$name = StringSplit($job.Name,",")
; here the name
Return $name[1]
EndFunc


;*******************************************
; check printes
;*******************************************
Func _StartPrinters($WMI)
; we're working with printers, Win32_Printer !!
$colInstalledPrinters = $WMI.ExecQuery("Select * from Win32_Printer")
; for each
For $objPrinter in $colInstalledPrinters
; is it in database?
_SQLite_Query(-1, "SELECT name FROM printers WHERE name = '" & $objPrinter.Name & "';", $hQuery)
; it is not
If _SQLite_FetchData($hQuery, $aRowPrinter, False, False) <> $SQLITE_OK Then
; zero for PDFCreator virtual printer, we will not lock
$cost = InputBox("New printer ", "Cost per page " & $objPrinter.Name & " :",1)
; in database with cost per page
_SQLite_Exec(-1, "INSERT INTO printers (name, cost) VALUES ('" & $objPrinter.Name & "'," & $cost & ");")
EndIf
; just to not have nasty Warning !!!
_SQLite_QueryFinalize ($hQuery)
; resume printer
$objPrinter.Resume()
Next
EndFunc


;*******************************************
; databas initialization
;*******************************************
Func _StartSQLITE()
; Does it really comment??
_SQLite_Startup()
; database is stored in file : "printers.db"
Global $Db = _SQLite_Open("printers.db")
; tables are created in the first use
_SQLite_Exec(-1, "CREATE TABLE IF NOT EXISTS jobs (document,owner,host,printer, pages, date);")
_SQLite_Exec(-1, "CREATE TABLE IF NOT EXISTS accounts (name,quota, used);")
_SQLite_Exec(-1, "CREATE TABLE IF NOT EXISTS printers (name,cost);")
EndFunc
Edited by Melba23
Posted corrected code and deleted all other posts

Share this post


Link to post
Share on other sites



online version is update, jobs table doesn't record printer's name.

jobs.DriverName is replaced by _PrinterName(jobs)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0