Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listprinters.tcl

(view source code of listprinters.tcl as plain text)

  1.  
  2. set errors 0
  3.  
  4. if { $argc == 0 } {
  5. 	set computer [string toupper [info hostname]]
  6. } elseif { [regexp {[;:/\?\*]} [lindex $argv 0]] }  {
  7. 	set errors 1
  8. } elseif { $argc == 1 } {
  9. 	set computer [string toupper [lindex $argv 0]]
  10. } else {
  11. 	set errors 1
  12. }
  13.  
  14. if { $errors != 1 } {
  15. 	package require twapi_wmi
  16. 	namespace path twapi
  17.  
  18. 	# Some basic error handling
  19. 	if { [catch {[set wbem_services [comobj_object winmgmts://$computer/root/cimv2]]} errmsg] } {
  20. 		# Work-around: when using catch, the "set wbem_services" command complains about
  21. 		# wrong arguments; if catch isn't used, however, that same "set wbem_services"
  22. 		# command continues without complaining (provided a valid computer is specified)
  23. 		if { $errorCode != "TCL WRONGARGS" } {
  24. 			puts "\nError: $errmsg"
  25. 			set errors 1
  26. 		}
  27. 	}
  28.  
  29. 	if { $errors != 1 } {
  30. 		set colitems [$wbem_services ExecQuery "SELECT * FROM Win32_Printer"]
  31.  
  32. 		if { [$colitems Count] == 0 } {
  33. 			puts "\nNo printers found on $computer"
  34. 			exit -1
  35. 		} elseif { [$colitems Count] == 1 } {
  36. 			puts "\n1 printer found on $computer:\n"
  37. 		} else {
  38. 			puts "\n[$colitems Count] printers found on $computer:\n"
  39. 		}
  40.  
  41. 		set printers {}
  42.  
  43. 		if { [$colitems Count] != 0 } {
  44. 			$colitems -iterate item {
  45. 				if { [$item Default] } {
  46. 					puts "\t[$item DeviceID]"
  47. 				} else {
  48. 					lappend printers [$item DeviceID]
  49. 				}
  50. 				$item -destroy
  51. 			}
  52.  
  53. 			foreach item [lsort -dictionary $printers] {
  54. 				puts "\t$item"
  55. 			}
  56.  
  57. 			$colitems -destroy
  58. 			$wbem_services -destroy
  59.  
  60. 			exit 0
  61. 		}
  62. 	}
  63. }
  64.  
  65. if { $errors == 1 } {
  66. 	puts "\nListPrinters.tcl,  Version 1.02"
  67. 	puts "List all printers available on the specified computer\n"
  68. 	puts "Usage:  tclsh.exe    ListPrinters.tcl   \[ computer \]\n"
  69. 	puts "Where:  \"computer\"   is the optional host name or IP address of a"
  70. 	puts "                     remote computer (default: local computer)\n"
  71. 	puts "Notes:  The default printer is listed first, followed by a sorted list"
  72. 	puts "        of the other printers."
  73. 	puts "        Return code is -1 in case of (command line) errors, otherwise 0.\n"
  74. 	puts "Written by Rob van der Woude"
  75. 	puts "http://www.robvanderwoude.com"
  76. 	exit -1
  77. }

page last modified: 2024-02-26; loaded in 0.0194 seconds