I'm writing a script to setup printers at a school that doesn't have a server on site.
The printers are networked and I don't want to share them out from a workstation because I don't want to worry
about the 10 user connection limit and whether or not the workstation is powered up.
And, anyway, if the printer has an ethernet connection, it should be put to use.

So far I've come up with the script below. I still have to figure out the best way to get the users to input
Options 1 - 5. I'm sure I'll find that in the manual or on this site. And I know I'll have to adjust the
positioning and dimensions of list boxes etc. What I'd like to know is if I have the functions laid out well.
Am I using a good strategy to complete the job.

Thanks for any and all input.

Code:
 

Break On
$System = CreateObject("Kixtart.System")

$Form = $System.Form()
$Form.Text = "St. Matthew School Computer Network - Admin Side Printer Install"
$Form.Width = 600
$Form.Height = 600
$Form.BackColor = $Form.RGB(255,255,0)

$panel = $form.Controls.Panel()
$panel.Dock = "top" ;resize for your needs
$panel.BackgroundImage = "\\s096-a0290-01\sch030$\printers\matthew.jpg"

$Instructions = $Form.Controls.Button()
$Instructions.Height = 31
$Instructions.Left = 10
$Instructions.Text = "Please wait while I determine which printers you are already connected to..."
$Instructions.Top = 135
$Instructions.Width 531

$listbox = $form.Controls.listbox()
$listbox.Height = 150
$listbox.Left = 280
$Listbox.Top = 175
$Listbox.Width = 250

$Exit = $Form.Controls.Button()
$Exit.Cancel = "True"
$Exit.Default = "True"
$Exit.Height = 31
$Exit.Left = 10
$Exit.Text = "Click Here To Close This Window"
$Exit.Top = 495
$Exit.Width = 531
$Exit.OnClick = "ExitButton()"

$Form.Center

$Form.Show

While $Form.Visible
$ = Execute($Form.DoEvents())
Loop
Exit 1

GetDefaultPrinter()
EnumPrinters()

;Use X as variable for printer choice
Dim X
Get $x
SelectAPrinter()

Function GetDefaultPrinter()
;FUNCTION GetDefaultPrinter()
;
;AUTHOR Jochen Polster (jochenDOTpolsterATgmxDOTnet)
;
;VERSION 1.0
;
;VERSION HISTORY 1.0 2004/04/28 Initial release
;
;ACTION Retrieves the current default Printer
;
;SYNTAX GetDefaultPrinter()
;
;PARAMETERS none
;
;REMARKS won't work with 9x OS
;
;RETURNS The current Users default Printer
;
;DEPENDENCIES None !
;
;EXAMPLES $default = GetDefaultPrinter

$GetDefaultPrinter =
Join(Split(ReadValue("HKEY_USERS\"+@sid+"\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),',',1),'')
$listbox2.additem("Default Printer = "+$GetDefaultPrinter)
;Create blank space between default printer listing and the listing of all printers.
$listbox2.additem("")
EndFunction

Function EnumPrinters()

;Writes all printer to a file.
;$array=printerlist()
;$rc = Open (1, "c:\installedprinters.txt", 5)
;For Each $printer in $array
; $rc = WriteLine (1, $printer + " is installed on: " + @WKSTA + @CRLF)
;Next
;$rc = Close(1)

;DISPLAY LOCAL AND NETWORKED PRINTERS

;shows the printers on your screen.
$array=printerlist()
For Each $printer in $array
$listbox2.additem("Printer = "+$printer)
Next

EndFunction

Function PrinterList(optional $remotepc, optional $displaymode)
;Function:
; PrinterList()
;
;Author:
; Allen Powell (Al_Po)
;
;Version:
; 1.3.1 (2006/05/10 fixed error detecting Print Servers sharing IP Printers)
;
;Revisions
; 1.3.0 (2005/10/21 added options to distinguish local or remote printers)
; 1.2.0 (2005/07/19 optimized code)
; 1.1.0 (2004/08/09 undimmed variable fix)
; 1.0.0 (2003/06/26 Original)
;
;Action:
; Creates an array/list of Printers, and optionally their ports installed, on a local or remote computer.
;
;Syntax:
; Printerlist(optional $remotepc,optional $displaymode)
;
;Parameters:
; $RemotePC: (Optional)
; Remote Computer. If omitted defaults to localpc.
; $DisplayMode (Optional)
; 0 - show all printers, don't display port info (Default)
; 1 - show all printers, display port info
; 2 - show local printers, don't display port info
; 3 - show local printers, display port info
; 4 - show remote printers, don't display port info
; 5 - show remote printers, display port info
;Returns:
; Array of Printers
;
;Dependencies:
; WMI
;
;Example:
;
;break on
;$RC=Setoption("WrapAtEOL","on")
;
;$array=printerlist()
;for each $printer in $array
; ? $printer
;next
;
;Source:
;
Dim $service,$printer,$printers,$printerdesc[0],$counter,$portname,$printername
If $remotepc=""
$remotepc="."
EndIf
$Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2")
If @error
Exit @error
EndIf
$Printers=$service.execquery ('select * from Win32_Printer')
For Each $printer in $printers
ReDim preserve $printerdesc[$counter]
If $displaymode & 1
$portname = "," + $printer.portname
EndIf
Select
Case $displaymode & 4 ;remote printers
If Left($printer.portname,2)="\\" Or Left($printer.name,2)=="\\"
$printername=$printer.name
EndIf
Case $displaymode & 2 ;local printers
If Left($printer.portname,2)<>"\\" And Left($printer.name,2)<>"\\"
$printername=$printer.name
EndIf
Case 1 ; all printers
$printername=$printer.name
EndSelect
If $printername<>""
$printerdesc[$counter]=$printername + $portname
$counter=$counter + 1
$printername=""
EndIf
Next
$PrinterList=$printerdesc
EndFunction

Function ExitButton()
Quit()
EndFunction


AddPrinter() - Add IP,LPT, Or COM Printers


--------------------------------------------------------------------------------

;Function:
; AddPrinter()
;
;Author:
; Allen Powell (Al_Po)
;
;Version:
; 1.0
;
;Action:
; Add IP,LPT, or COM Printers locally or remotely. Also, add PrintServer connections locally.
;
;Syntax:
; AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default)
;
;Parameters:
; $Port: (required)
; IPAddress (xxx.xxx.xxx.xxx)
; LPT (LPT1:)
; COM (COM1:)
; PrintServer (\\server\printer)
; $PrinterType: (required)
; Descriptor of Printer within INF file
; Note: If using a PrintServer, $PrinterType value is ignored.
; $DriverInf: (Optional)
; Location and name of INF file containing print drivers
; Note: If omitted, attempts to use default Windows Print Drivers
; $PrinterDesc: (Optional)
; Description of Printer
; Note: If ommitted, $PrinterDesc = PrinterType
; $RemotePC: (Optional)
; PC to install Printer/Drivers on. If omitted defaults to localpc.
; Note: Ignored if $Port is a PrintServer
; $Default: (Optional)
; 0 = Do not Set as Default Printer (Default)
; 1 = Set as Default Printer
;
;Returns:
; 1 - Successful
; 0 - Failed
; If Failed sets @error to
; -4 - Error connecting to RemotePC
; -3 - Could not open/find INF file
; -2 - Could not find $PrinterType in INF file
; -1 - Unrecognized Port
;
;Dependencies:
; Windows 2000 Professional and up
; rundll32 printui.dll,PrintUIEntry
;
;Examples:
;$=AddPrinter("192.168.1.1","Xerox DocumentCenter 555 PCL6","\\Server\PrinterDrives\Xerox\dc555pcl\dc555x.inf","Xerox" +
" Document Centre 55 5 PCL6 in Office",REMOTEPC,1)
;$=Addprinter("LPT1","HP LaserJet 8100 Series PS",,"HP8100 in IS(test)")
;$=AddPrinter("192.168.1.1","HP LaserJet 8100 Series PS",,"HP8100 in IS(test)",,1)
;$=AddPrinter('\\server\printer','_')
;Source:

Function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default)
Dim $quitloop,$IPPrinterKey,$PrinterCMD
$AddPrinter=0
If Not Left($Port,2)="\\"
If $Driverinf=""
$DriverInf="%windir%\inf\ntprint.inf"
EndIf
If Not Exist($DriverInf)
Exit -3
EndIf
Shell '%comspec% /c type "$driverinf">%temp%\addprinter.txt'
If Open(3,"%temp%\addprinter.txt",0)=0
$quitloop=0
While @error=0 And $quitloop=0
$line=ReadLine(3)
$quitloop=InStr($line,$PrinterType)
Loop
$=Close(3)
If $quitloop=0
Exit -2
EndIf
Else
Exit -3
EndIf
EndIf
If $PrinterDesc=""
$PrinterDesc=$PrinterType
EndIf
If $remotepc=""
$remotepc='\\'+ @wksta
Else
If Not Left($remotepc,2)="\\"
$remotepc='\\' + $remotepc
EndIf
EndIf

Select
Case Left($Port,2)="\\"
$printserver=1
$printerdesc=$port
Case InStr($Port,".")
$IPPrinterKey = '$remotepc\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors'+
'\Standard TCP/IP Port\Ports\IP_' +$Port
If Not KeyExist($IPPrinterKey)
$=AddKey($IPPrinterKey)
$=WriteValue($IPPrinterKey,"Protocol","00000001","REG_DWORD")
$=WriteValue($IPPrinterKey,"Version","00000001","REG_DWORD")
$=WriteValue($IPPrinterKey,"HostName","", "REG_SZ")
$=WriteValue($IPPrinterKey,"IPAddress","$Port","REG_SZ")
$=WriteValue($IPPrinterKey,"HWAddress","","REG_SZ")
$=WriteValue($IPPrinterKey,"PortNumber","00009100","REG_DWORD")
$=WriteValue($IPPrinterKey,"SNMP Community","public","REG_SZ")
$=WriteValue($IPPrinterKey,"SNMP Enabled","00000001","REG_DWORD")
$=WriteValue($IPPrinterKey,"SNMP Index","00000001","REG_DWORD")
EndIf
$objects=GetObject("winmgmts:{impersonationLevel=impersonate}!"+$remotepc+"\root\cimv2")
If Not @error=0
Exit -4
EndIf
$services=$objects.ExecQuery('Select * from Win32_Service WHERE Name = "Spooler"')
For Each $spooler in $services
$=Execute('$$=$$spooler.stopservice()')
Next
Sleep 1
For Each $spooler in $services
$=Execute('$$=$$spooler.startservice()')
Next
$port='IP_$port'
Case UCase(Left($Port,3))="LPT" Or UCase(Left($Port,3))="COM"
If Not Right($port,1)=":"
$Port=$port+":"
EndIf
If Not Val(Left(Right($port,2),1))>=1
Exit -1
EndIf
Case UCase(Left($Port,4))="FILE"
If Not Right($port,1)=":"
$Port=$port+":"
EndIf
Case 1
Exit -1
EndSelect
If Not $Printserver=1
$printercmd='rundll32 printui.dll,PrintUIEntry /if /b "$printerdesc" /c "$remotepc" ' +
'/f "$driverinf" /r "$port" /m "$Printertype" /z /u '
Shell $printercmd
Else
If Not AddPrinterConnection($port)=0
Exit @error
EndIf
EndIf
If $default=1
If Not SetDefaultPrinter($PrinterDesc)=0
Exit @error
EndIf
EndIf
$AddPrinter=1
EndFunction
Function SelectAPrinter()

Select

Case x=1
Function AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0)

Case x=2
Function AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1)

Case x=3
Function AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0)

Case x=4
Function AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1)

Case x=5 Function
Function AddPrinter("10.48.18.3","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1)

EndSelect
EndFunction



[Long lines removed by NTDOC]


Edited by NTDOC (2006-10-01 04:35 AM)