|
Script to setup printers
|
|
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]
|
|
Re: Script to setup printers
|
|
An alternative woudl be to set up a Linux print server together with Samba. You basically do a one-time install of Linux, connect to the printers, install Samba, and the Windows machines can connect to the print server, incl. pullling down the printer drivers. See for example http://www.linuxforums.org/servers/howto:_fileserver_with_samba_and_printserver_with_cups.html
|
|
Re: Script to setup printers
|
|
Thanks for the suggestion Jens. What you say is true. However, then I have to learn how to use another operating system. I have to provide excellent documentation in case I go on leave or I leave the job. I have to find a computer for the job and I have to make sure the computer is always up and running. And since the ethernet is already connected to the printer, I think adding one more piece of hardware to the chain is just another link in the chain that could break down. I think it would be more worth my while to spend two hours touching each computer.
But thanks anyway...
|
Benny69
|
(MM club member)
|
2006-09-30 05:18 PM
|
|
|
|
|
Re: Script to setup printers
|
|
Hi jeremy, I have added an error check to validate the installation and functionality of KiXforms with these lines: Code:
If Not $System $nul= MessageBox("KiXforms Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
I moved the function calls before the While Loop, if they were after the While Loop they would not be called until after the form closed.
I modified the ‘SelectAPrinter’ function, the only time you use the word ‘Funtion’ is to define a function. Code:
Function SelectAPrinter()
Select Case x=1 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case x=2 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case x=3 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case x=4 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case x=5 AddPrinter("10.48.18.3","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) EndSelect EndFunction
Here is you updated code: Code:
Break On $System = CreateObject("Kixtart.System") If Not $System $nul= MessageBox("KiXforms Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
$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
GetDefaultPrinter() EnumPrinters()
Use X as variable For printer choice Dim X Get $x
SelectAPrinter()
While $Form.Visible $ = Execute($Form.DoEvents()) Loop Exit 1
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.&n bsp; ; ;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
Function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default) ;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: 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 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case x=2 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case x=3 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case x=4 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case x=5 AddPrinter("10.48.18.3","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) EndSelect EndFunction
You could select the printer(s) to install by the IP of the workstation with a select case and an IP range of the 4th Octet with something like this: Code:
Function SelectAPrinter() Dim $FourthOctet $Octets = Split(@IPADDRESS0,'.') $FourthOctet = Val($Octets[3]) Select Case ($FourthOctet >= 1 And $FourthOctet <= 31) ; 10.48.18.1 10.48.18.31 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case ($FourthOctet >= 32 And $FourthOctet <= 63) ; 10.48.18.32 10.48.18.63 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case ($FourthOctet >= 64 And $FourthOctet <= 127) ; 10.48.18.64 10.48.18.127 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case ($FourthOctet >= 128 And $FourthOctet <= 191) ;10.48.18.128 10.48.18.191 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case ($FourthOctet >= 192 And $FourthOctet <= 223) ;10.48.18.192 10.48.18.223 AddPrinter("10.48.18.3","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) EndSelect EndFunction
You would have to modify the IP range of course, but it should give you the idea.
[Long lines removed by NTDOC]
|
|
Re: Script to setup printers
|
|
Thanks for the advice Benny. In your example of SelectAPrinter, your using the workstations ip address to determine which printer to hook up to? Thanks, but that's not quite how I'm going to work it. There are three printers. Teachers are allowed to choose either the library or computer lab to be their default printer. They can add the office printer but it's not allowed to be their default printer. So I'm planning on just inserting a message box that says:
Enter 1 to have the Library printer be your default printer Enter 2 to have the Library printer as just a printer Enter 3 to have the Computer Lab printer be your default printer Enter 4 to have the Computer Lab printer as just a printer Enter 5 to have the Office printer as just a printer.
Thanks for your other suggestions though. (And if you have any ideas for improving the language of the lines that say 'just a printer', that would be welcome too).
Jeremy
|
Gargoyle
|
(MM club member)
|
2006-10-01 03:05 AM
|
|
|
|
|
Re: Script to setup printers
|
|
Why not have two drop down list boxes. One for the default printer and one for all others. Make the default printer list non multi select, and the other can be multi select so that can set up multiple printers in one shot.
You would of course want to build in some checks to make it so that they can not select the same printer in both lists.
|
|
Re: Script to setup printers
|
|
That's an interesting idea Gargoyle. Thanks. But before I do that, I need to ask for some help with my code again. I removed the section displaying what printers are already set up and which is the default. I figure the user should already know that info. But now I'm having trouble showing my 'Choose Printer' text box. I looked at the kixforms on line help and saw that I could add multiline and scroll bar elements to my properties. But obviously I'm not doing it right. Can someone please provide some direction?
Code:
;region Script Settings ;kix32.exe ; ;%TEMP% ;C:\Documents and Settings\Jeremy Schubert\My Documents\AdminScriptEditor ; ;True ; ;True ; ;False ;True ;True ;False ; ; ;True ; ; ;1.0.0.1 ; ; ; ; ; ; ; ;endregion
; **************** Variable Settings ************** Break On $System = CreateObject("Kixtart.System") If Not $System $nul= MessageBox("KiXforms Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
$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"
$PrinterChoices = $Form.Controls.Button() $PrinterChoices.Height = 450 $PrinterChoices.Left = 10 $PrinterChoices.Multiline = True $PrinterChoices.Text = "Please enter a printer selection" + Chr(13) + Chr(10) + "Enter 1 to setup the Library Printer" + Chr(13) + Chr(10) + "Enter 2 to setup the Library Printer as your default printer" + Chr(13) + Chr(10) + "Enter 3 to setup the Computer Lab Printer" + Chr(13) + Chr(10) + "Enter 4 to setup the Computer Lab Printer as your default printer" + Chr(13) + Chr(10) + "Enter 5 to setup the Office computer","Printer Choice" $PrinterChoices.Top = 25 $PrinterChoices.Width 400
$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
;x is the variable that represents the user's choice of printer
Dim $X Get $x
SelectAPrinter()
While $Form.Visible $ = Execute($Form.DoEvents()) Loop Exit 1
Function SelectAPrinter()
Select Case x=1 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case x=2 AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case x=3 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case x=4 AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case x=5 AddPrinter("10.48.18.3","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) EndSelect EndFunction
[Long lines removed by NTDOC]
|
Witto
|
(MM club member)
|
2006-10-01 08:49 AM
|
|
|
|
|
Re: Script to setup printers
|
|
Why do you ask for input via a command window when you are using a KiXforms graphic interface? Code:
;x is the variable that represents the user's choice of printer Dim $X Get $x
I presume you want to change this: Code:
$PrinterChoices.Width = 400
You get the background image and the driver from the same computer I presume. I think there is a typo. But I also presume this is a workstation. Doesn't the 10 computer limit also goes for this one? Code:
$panel.BackgroundImage = "\\s096-a0290-01\sch030$\printers\matthew.jpg" ... AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0)
|
Gargoyle
|
(MM club member)
|
2006-10-01 09:18 AM
|
|
|
|
|
Re: Script to setup printers
|
|
Well to begin with, you have your "text" box defined as a button ... Code:
$PrinterChoices = $Form.Controls.Button()
You really should take a step back and begin the form all over, it seems you have lost track of where you are trying to go with it...
Here is an example of a form that I would use (note this is KiXforms.net) Code:
Break On $System = CreateObject("Kixforms.System") If Not $System $nul= MessageBox("KiXforms.Net Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
$Form3 = $System.Form() $Form3.Left = 325 $Form3.StartPosition = 0 ;FormStartPosition_Manual $Form3.Size = $System.Size(323,161) ;(Width,Height) $Form3.Text = "Printer Setup" $Form3.Top = 75
$Label1 = $System.Label() $Label1.BorderStyle = 1 ;FixedSingle $Label1.Left = 9 $Label1.Text = "Select Default Printer" $Label1.Top = 9 $Label1.Width = 123 $nul = $Form3.Controls.Add($Label1)
$Label2 = $System.Label() $Label2.BorderStyle = 1 ;FixedSingle $Label2.Left = 162 $Label2.Text = "Select other printers" $Label2.Top = 8 $Label2.Width = 111 $nul = $Form3.Controls.Add($Label2)
$ComboBox1 = $System.ComboBox() $ComboBox1.Left = 8 $ComboBox1.Text = "ComboBox1" $ComboBox1.Top = 38 $nul = $Form3.Controls.Add($ComboBox1)
$ComboBox1Item0 = $ComboBox1.Items.Add("Line1") $ComboBox1Item1 = $ComboBox1.Items.Add("Line2") $ComboBox1Item2 = $ComboBox1.Items.Add("Line3")
$ComboBox2 = $System.ComboBox() $ComboBox2.Left = 160 $ComboBox2.Text = "ComboBox2" $ComboBox2.Top = 39 $nul = $Form3.Controls.Add($ComboBox2)
$ComboBox2Item0 = $ComboBox2.Items.Add("Line1") $ComboBox2Item1 = $ComboBox2.Items.Add("Line2") $ComboBox2Item2 = $ComboBox2.Items.Add("Line3")
$Button1 = $System.Button() $Button1.Left = 8 $Button1.Text = "Set Printers" $Button1.Top = 84 $nul = $Form3.Controls.Add($Button1)
$Button2 = $System.Button() $Button2.Left = 109 $Button2.Text = "Clear" $Button2.Top = 84 $nul = $Form3.Controls.Add($Button2)
$Button3 = $System.Button() $Button3.Left = 210 $Button3.Text = "Exit" $Button3.Top = 84 $nul = $Form3.Controls.Add($Button3)
$Form3.Show ;Displays the Form
While $Form3.Visible $Nul = Execute($Form3.DoEvents()) Loop Exit 0
Please note this is nowhere near a working script. Just an idea of what your form should look like.
|
|
Re: Script to setup printers
|
|
Thanks for the advice Gargoyle and Witto. I have a lot to learn! Witto - The jpg and inf files are actually hosted on a downtown server so I don't have to worry about the 10 user limit. Gargoyle - I'm examining your code to see how I would write similar for my needs.
Thanks again to you both, Jeremy
|
Benny69
|
(MM club member)
|
2006-10-01 05:35 PM
|
|
|
|
|
Re: Script to setup printers
|
|
Hi Jeremy, I have made several changes;
There are 2 ‘RadioButtons': ‘Use Current Printer Settings’ – Select this one and no printer settings will be changed ‘Modify Printer Settings’ – Select this one and selections in the ‘GroupBox’ will be appied.
Inside the ‘GroupBox’; Select the ‘RadioButton’ to the left of the Computer Lab Printer and it will be set as the default printer.
Select the ‘RadioButton’ to the left of the Library Printer and it will be set as the default printer.
Place a check next to each printer you want to install.
Click the ‘Click Here To Close This Window’ and the chosen printer settings will be applied.
Here is your modified code: Code:
Break On $System = CreateObject("Kixtart.System") If Not $System $nul= MessageBox("KiXforms Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
$Form = $System.Form() $Form.Text = "St. Matthew School Computer Network - Admin Site Printer Install" $Form.Width = 600 $Form.Height = 600 $Form.BackColor = $Form.RGB(255,255,0)
$Panel = $Form.Controls.Add("Panel") $Panel.Dock = "top" ;resize for your needs $Panel.BackgroundImage = "\\s096-a0290-01\sch030$\printers\matthew.jpg"
$Instructions = $Form.Controls.Add("Label") $Instructions.Height = 31 $Instructions.Left = 10 $Instructions.TextAlign = 32 ;Center Text in Label $Instructions.Text = "Please wait while I determine which printers you are already connected to..." $Instructions.Top = 135 $Instructions.Width = $Form.ClientWidth-20
$Listbox = $Form.Controls.Add("Listbox") $Listbox.Height = 150 $Listbox.Left = 280 $Listbox.Top = 175 $Listbox.Width = 250
$DefPrintersRadioButton = $Form.Controls.Add("RadioButton") $DefPrintersRadioButton.Left = 10 $DefPrintersRadioButton.Top = $Listbox.Bottom+10 $DefPrintersRadioButton.Text = "Use Current Printer Settings" $DefPrintersRadioButton.Width = 150 $DefPrintersRadioButton.Checked = "True" $DefPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$ModPrintersRadioButton = $Form.Controls.Add("RadioButton") $ModPrintersRadioButton.Left = 10 $ModPrintersRadioButton.Top = $DefPrintersRadioButton.Bottom+10 $ModPrintersRadioButton.Text = "Modify Printer Settings" $ModPrintersRadioButton.Width = 150 $ModPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$PrintersGroupBox = $Form.Controls.Add("GroupBox") $PrintersGroupBox.Left = $ModPrintersRadioButton.Right+10 $PrintersGroupBox.Top = $ModPrintersRadioButton.Top $PrintersGroupBox.Height = 85 $PrintersGroupBox.Width = $Form.ClientWidth-180 $PrintersGroupBox.Text = "Default Install Printer"
$ComputerLabRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $ComputerLabRadioButton.Left = 20 $ComputerLabRadioButton.Top = 20 $ComputerLabRadioButton.Height = 20 $ComputerLabRadioButton.Width = 20 $ComputerLabRadioButton.Enabled = "False" $ComputerLabRadioButton.OnClick = "CheckComputerLabRadioButton()"
$ComputerLabCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $ComputerLabCheckBox.Left = $ComputerLabRadioButton.Right+20 $ComputerLabCheckBox.Top = $ComputerLabRadioButton.Top $ComputerLabCheckBox.Height = 20 $ComputerLabCheckBox.Width = $PrintersGroupBox.Width-$ComputerLabCheckBox.Left-10 $ComputerLabCheckBox.Text = "Computer Lab Printer" $ComputerLabCheckBox.Enabled = "False" $ComputerLabCheckBox.OnClick = "CheckComputerLabCheckBox()"
$LibraryPrinterRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $LibraryPrinterRadioButton.Left = 20 $LibraryPrinterRadioButton.Top = $ComputerLabRadioButton.Bottom $LibraryPrinterRadioButton.Height = 20 $LibraryPrinterRadioButton.Width = 20 $LibraryPrinterRadioButton.Enabled = "False" $LibraryPrinterRadioButton.OnClick = "CheckLibraryPrinterRadioButton()"
$LibraryPrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $LibraryPrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $LibraryPrinterCheckBox.Top = $LibraryPrinterRadioButton.Top $LibraryPrinterCheckBox.Height = 20 $LibraryPrinterCheckBox.Width = $PrintersGroupBox.Width-$LibraryPrinterCheckBox.Left-10 $LibraryPrinterCheckBox.Text = "Library Printer" $LibraryPrinterCheckBox.Enabled = "False" $LibraryPrinterCheckBox.OnClick = "CheckLibraryPrinterCheckBox()"
$OfficePrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $OfficePrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $OfficePrinterCheckBox.Top = $LibraryPrinterCheckBox.Bottom $OfficePrinterCheckBox.Height = 20 $OfficePrinterCheckBox.Width = $PrintersGroupBox.Width-$OfficePrinterCheckBox.Left-10 $OfficePrinterCheckBox.Text = "Office Printer" $OfficePrinterCheckBox.Enabled = "False"
$Exit = $Form.Controls.Add("Button") $Exit.Cancel = "True" $Exit.Default = "True" $Exit.Height = 31 $Exit.Left = 10 $Exit.Text = "Click Here To Close This Window" $Exit.Top = $PrintersGroupBox.Bottom+10 $Exit.Width = $Form.ClientWidth-20 $Exit.OnClick = "ExitButton()"
$Form.Center $Form.Show
GetDefaultPrinter() EnumPrinters()
While $Form.Visible $nul = Execute($Form.DoEvents) Loop Exit 1
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.&n bsp; ; ;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() SelectAPrinter() Quit() EndFunction
Function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default) ;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: 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()
If $ModPrintersRadioButton.Checked = "True" Select Case ($LibraryPrinterRadioButton.Checked = "False") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case ($LibraryPrinterRadioButton.Checked = "True") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case ($ComputerLabRadioButton.Checked = "False") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case ($ComputerLabRadioButton.Checked = "True") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case $OfficePrinterCheckBox.CheckState = -1 AddPrinter("10.48.18.3","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Office Printer",0) EndSelect EndIf EndFunction
Function CheckPrinterRadioButtons() If $DefPrintersRadioButton.Checked = "True" $ComputerLabRadioButton.Enabled = "True" $LibraryPrinterRadioButton.Enabled = "True" $ComputerLabCheckBox.Enabled = "True" $LibraryPrinterCheckBox.Enabled = "True" $OfficePrinterCheckBox.Enabled = "True" $ComputerLabRadioButton.Checked = "True" Else $ComputerLabRadioButton.Enabled = "False" $LibraryPrinterRadioButton.Enabled = "False" $ComputerLabCheckBox.Enabled = "False" $LibraryPrinterCheckBox.Enabled = "False" $OfficePrinterCheckBox.Enabled = "False" $ComputerLabRadioButton.Checked = "False" $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.CheckState = 0 $LibraryPrinterCheckBox.CheckState = 0 $OfficePrinterCheckBox.CheckState = 0 EndIf EndFunction
Function CheckComputerLabRadioButton() $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.Checked = "True" EndFunction
Function CheckLibraryPrinterRadioButton() $ComputerLabRadioButton.Checked = "False" $LibraryPrinterCheckBox.Checked = "True" EndFunction
Function CheckComputerLabCheckBox() If $LibraryPrinterRadioButton.Checked = "True" $ComputerLabCheckBox.Checked = "True" EndIf EndFunction
Function CheckLibraryPrinterCheckBox() If $ComputerLabRadioButton.Checked = "True" $LibraryPrinterCheckBox.Checked = "True" EndIf EndFunction
|
Witto
|
(MM club member)
|
2006-10-01 11:52 PM
|
|
|
|
|
Re: Script to setup printers
|
|
That is quite a change. Could I suggest some changes? Maybe an indication to the user when the listbox is populated. Also silence the screenoutput for GetDefaultPrinter(). Code:
$Form.Center $Form.Show
$RC = GetDefaultPrinter() EnumPrinters()
$Instructions.Text = "Your printers are listed. Now you can add extra printers and change the default."
While $Form.Visible $nul = Execute($Form.DoEvents) Loop Exit 1
The ListBox does not get populated because $Listbox2 is being referenced. Code:
$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("") ... $Listbox2.additem("Printer = "+$printer)
|
Benny69
|
(MM club member)
|
2006-10-02 12:23 AM
|
|
|
|
|
Re: Script to setup printers
|
|
good points witto, here is the modified code: Code:
Break On $System = CreateObject("Kixtart.System") If Not $System $nul= MessageBox("KiXforms Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
$Form = $System.Form() $Form.Text = "St. Matthew School Computer Network - Admin Site Printer Install" $Form.Width = 600 $Form.Height = 600 $Form.BackColor = $Form.RGB(255,255,0)
$Panel = $Form.Controls.Add("Panel") $Panel.Dock = "top" ;resize for your needs $Panel.BackgroundImage = "\\s096-a0290-01\sch030$\printers\matthew.jpg"
$Instructions = $Form.Controls.Add("Label") $Instructions.Height = 31 $Instructions.Left = 10 $Instructions.TextAlign = 32 ;Center Text in Label $Instructions.Text = "Please wait while I determine which printers you are already connected to..." $Instructions.Top = 135 $Instructions.Width = $Form.ClientWidth-20
$Listbox = $Form.Controls.Add("Listbox") $Listbox.Height = 150 $Listbox.Left = 280 $Listbox.Top = 175 $Listbox.Width = 250
$DefPrintersRadioButton = $Form.Controls.Add("RadioButton") $DefPrintersRadioButton.Left = 10 $DefPrintersRadioButton.Top = $Listbox.Bottom+10 $DefPrintersRadioButton.Text = "Use Current Printer Settings" $DefPrintersRadioButton.Width = 150 $DefPrintersRadioButton.Checked = "True" $DefPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$ModPrintersRadioButton = $Form.Controls.Add("RadioButton") $ModPrintersRadioButton.Left = 10 $ModPrintersRadioButton.Top = $DefPrintersRadioButton.Bottom+10 $ModPrintersRadioButton.Text = "Modify Printer Settings" $ModPrintersRadioButton.Width = 150 $ModPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$PrintersGroupBox = $Form.Controls.Add("GroupBox") $PrintersGroupBox.Left = $ModPrintersRadioButton.Right+10 $PrintersGroupBox.Top = $ModPrintersRadioButton.Top $PrintersGroupBox.Height = 85 $PrintersGroupBox.Width = $Form.ClientWidth-180 $PrintersGroupBox.Text = "Default Install Printer"
$ComputerLabRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $ComputerLabRadioButton.Left = 20 $ComputerLabRadioButton.Top = 20 $ComputerLabRadioButton.Height = 20 $ComputerLabRadioButton.Width = 20 $ComputerLabRadioButton.Enabled = "False" $ComputerLabRadioButton.OnClick = "CheckComputerLabRadioButton()"
$ComputerLabCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $ComputerLabCheckBox.Left = $ComputerLabRadioButton.Right+20 $ComputerLabCheckBox.Top = $ComputerLabRadioButton.Top $ComputerLabCheckBox.Height = 20 $ComputerLabCheckBox.Width = $PrintersGroupBox.Width-$ComputerLabCheckBox.Left-10 $ComputerLabCheckBox.Text = "Computer Lab Printer" $ComputerLabCheckBox.Enabled = "False" $ComputerLabCheckBox.OnClick = "CheckComputerLabCheckBox()"
$LibraryPrinterRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $LibraryPrinterRadioButton.Left = 20 $LibraryPrinterRadioButton.Top = $ComputerLabRadioButton.Bottom $LibraryPrinterRadioButton.Height = 20 $LibraryPrinterRadioButton.Width = 20 $LibraryPrinterRadioButton.Enabled = "False" $LibraryPrinterRadioButton.OnClick = "CheckLibraryPrinterRadioButton()"
$LibraryPrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $LibraryPrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $LibraryPrinterCheckBox.Top = $LibraryPrinterRadioButton.Top $LibraryPrinterCheckBox.Height = 20 $LibraryPrinterCheckBox.Width = $PrintersGroupBox.Width-$LibraryPrinterCheckBox.Left-10 $LibraryPrinterCheckBox.Text = "Library Printer" $LibraryPrinterCheckBox.Enabled = "False" $LibraryPrinterCheckBox.OnClick = "CheckLibraryPrinterCheckBox()"
$OfficePrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $OfficePrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $OfficePrinterCheckBox.Top = $LibraryPrinterCheckBox.Bottom $OfficePrinterCheckBox.Height = 20 $OfficePrinterCheckBox.Width = $PrintersGroupBox.Width-$OfficePrinterCheckBox.Left-10 $OfficePrinterCheckBox.Text = "Office Printer" $OfficePrinterCheckBox.Enabled = "False"
$Exit = $Form.Controls.Add("Button") $Exit.Cancel = "True" $Exit.Default = "True" $Exit.Height = 31 $Exit.Left = 10 $Exit.Text = "Click Here To Close This Window" $Exit.Top = $PrintersGroupBox.Bottom+10 $Exit.Width = $Form.ClientWidth-20 $Exit.OnClick = "ExitButton()"
$Form.Center $Form.Show
$nul = GetDefaultPrinter() EnumPrinters() $Instructions.Text = "Your printers are listed. Now you can add extra printers and change the default."
While $Form.Visible $nul = Execute($Form.DoEvents) Loop Exit 1
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),'') $Listbox.additem("Default Printer = "+$GetDefaultPrinter) ;Create blank space between default printer listing and the listing of all printers. $Listbox.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 $Listbox.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.&n bsp; ; ;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() SelectAPrinter() Quit() EndFunction
Function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default) ;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: 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()
If $ModPrintersRadioButton.Checked = "True" Select Case ($LibraryPrinterRadioButton.Checked = "False") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case ($LibraryPrinterRadioButton.Checked = "True") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.18.1","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case ($ComputerLabRadioButton.Checked = "False") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case ($ComputerLabRadioButton.Checked = "True") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.18.2","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case $OfficePrinterCheckBox.CheckState = -1 AddPrinter("10.48.18.3","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Office Printer",0) EndSelect EndIf EndFunction
Function CheckPrinterRadioButtons() If $DefPrintersRadioButton.Checked = "True" $ComputerLabRadioButton.Enabled = "True" $LibraryPrinterRadioButton.Enabled = "True" $ComputerLabCheckBox.Enabled = "True" $LibraryPrinterCheckBox.Enabled = "True" $OfficePrinterCheckBox.Enabled = "True" $ComputerLabRadioButton.Checked = "True" Else $ComputerLabRadioButton.Enabled = "False" $LibraryPrinterRadioButton.Enabled = "False" $ComputerLabCheckBox.Enabled = "False" $LibraryPrinterCheckBox.Enabled = "False" $OfficePrinterCheckBox.Enabled = "False" $ComputerLabRadioButton.Checked = "False" $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.CheckState = 0 $LibraryPrinterCheckBox.CheckState = 0 $OfficePrinterCheckBox.CheckState = 0 EndIf EndFunction
Function CheckComputerLabRadioButton() $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.Checked = "True" EndFunction
Function CheckLibraryPrinterRadioButton() $ComputerLabRadioButton.Checked = "False" $LibraryPrinterCheckBox.Checked = "True" EndFunction
Function CheckComputerLabCheckBox() If $LibraryPrinterRadioButton.Checked = "True" $ComputerLabCheckBox.Checked = "True" EndIf EndFunction
Function CheckLibraryPrinterCheckBox() If $ComputerLabRadioButton.Checked = "True" $LibraryPrinterCheckBox.Checked = "True" EndIf EndFunction
|
|
Re: Script to setup printers
|
|
Thank you all for your help. I'll test it out on Tuesday when I return to work. You've all been very helpful. When I become a lot more experienced with this, I hope I'll be able to help other newbies as well. Jeremy
|
|
Re: Script to setup printers
|
|
Hey Benny, I've tried out the code this morning, but it keeps failing telling me that selectaprinter() has no endfunction. But from what I see in your code, there is an endfunction associated with selectaprinter. I've tried remming out a few things with out any luck. Do you have any suggestions for me? Could it just be my computer??? Thanks, Jeremy
|
Benny69
|
(MM club member)
|
2006-10-03 05:06 PM
|
|
|
|
|
Re: Script to setup printers
|
|
have you altered the code that i posted in any way, if you have post the the code you are using.
|
NTDOC
|
(KiX Master)
|
2006-10-03 07:38 PM
|
|
|
|
|
Re: Script to setup printers
|
|
Maybe I broke the code by breaking the long lines.
|
|
Re: Script to setup printers
|
|
Sorry guys! I thought I reposted the code. But I guess not. Here it is. Any help would be appreciated. Thanks, Jeremy
Code:
Break On $System = CreateObject("Kixtart.System") If Not $System $nul= MessageBox("KiXforms Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
$Form = $System.Form() $Form.Text = "St. Matthew School Computer Network - Admin Site Printer Install" $Form.Width = 600 $Form.Height = 600 $Form.BackColor = $Form.RGB(255,255,0)
$Panel = $Form.Controls.Add("Panel") $Panel.Dock = "top" ;resize for your needs $Panel.BackgroundImage = "\\s096-a0290-01\sch030$\printers\matthew.jpg"
$Instructions = $Form.Controls.Add("Label") $Instructions.Height = 31 $Instructions.Left = 10 $Instructions.TextAlign = 32 ;Center Text in Label $Instructions.Text = "Please wait while I determine which printers you are already connected to..." $Instructions.Top = 135 $Instructions.Width = $Form.ClientWidth-20
$Listbox = $Form.Controls.Add("Listbox") $Listbox.Height = 150 $Listbox.Left = 280 $Listbox.Top = 175 $Listbox.Width = 250
$DefPrintersRadioButton = $Form.Controls.Add("RadioButton") $DefPrintersRadioButton.Left = 10 $DefPrintersRadioButton.Top = $Listbox.Bottom+10 $DefPrintersRadioButton.Text = "Use Current Printer Settings" $DefPrintersRadioButton.Width = 150 $DefPrintersRadioButton.Checked = "True" $DefPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$ModPrintersRadioButton = $Form.Controls.Add("RadioButton") $ModPrintersRadioButton.Left = 10 $ModPrintersRadioButton.Top = $DefPrintersRadioButton.Bottom+10 $ModPrintersRadioButton.Text = "Modify Printer Settings" $ModPrintersRadioButton.Width = 150 $ModPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$PrintersGroupBox = $Form.Controls.Add("GroupBox") $PrintersGroupBox.Left = $ModPrintersRadioButton.Right+10 $PrintersGroupBox.Top = $ModPrintersRadioButton.Top $PrintersGroupBox.Height = 85 $PrintersGroupBox.Width = $Form.ClientWidth-180 $PrintersGroupBox.Text = "Default Install Printer"
$ComputerLabRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $ComputerLabRadioButton.Left = 20 $ComputerLabRadioButton.Top = 20 $ComputerLabRadioButton.Height = 20 $ComputerLabRadioButton.Width = 20 $ComputerLabRadioButton.Enabled = "False" $ComputerLabRadioButton.OnClick = "CheckComputerLabRadioButton()"
$ComputerLabCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $ComputerLabCheckBox.Left = $ComputerLabRadioButton.Right+20 $ComputerLabCheckBox.Top = $ComputerLabRadioButton.Top $ComputerLabCheckBox.Height = 20 $ComputerLabCheckBox.Width = $PrintersGroupBox.Width-$ComputerLabCheckBox.Left-10 $ComputerLabCheckBox.Text = "Computer Lab Printer" $ComputerLabCheckBox.Enabled = "False" $ComputerLabCheckBox.OnClick = "CheckComputerLabCheckBox()"
$LibraryPrinterRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $LibraryPrinterRadioButton.Left = 20 $LibraryPrinterRadioButton.Top = $ComputerLabRadioButton.Bottom $LibraryPrinterRadioButton.Height = 20 $LibraryPrinterRadioButton.Width = 20 $LibraryPrinterRadioButton.Enabled = "False" $LibraryPrinterRadioButton.OnClick = "CheckLibraryPrinterRadioButton()"
$LibraryPrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $LibraryPrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $LibraryPrinterCheckBox.Top = $LibraryPrinterRadioButton.Top $LibraryPrinterCheckBox.Height = 20 $LibraryPrinterCheckBox.Width = $PrintersGroupBox.Width-$LibraryPrinterCheckBox.Left-10 $LibraryPrinterCheckBox.Text = "Library Printer" $LibraryPrinterCheckBox.Enabled = "False" $LibraryPrinterCheckBox.OnClick = "CheckLibraryPrinterCheckBox()"
$OfficePrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $OfficePrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $OfficePrinterCheckBox.Top = $LibraryPrinterCheckBox.Bottom $OfficePrinterCheckBox.Height = 20 $OfficePrinterCheckBox.Width = $PrintersGroupBox.Width-$OfficePrinterCheckBox.Left-10 $OfficePrinterCheckBox.Text = "Office Printer" $OfficePrinterCheckBox.Enabled = "False"
$Exit = $Form.Controls.Add("Button") $Exit.Cancel = "True" $Exit.Default = "True" $Exit.Height = 31 $Exit.Left = 10 $Exit.Text = "Click Here To Close This Window" $Exit.Top = $PrintersGroupBox.Bottom+10 $Exit.Width = $Form.ClientWidth-20 $Exit.OnClick = "ExitButton()"
$Form.Center $Form.Show
$nul = GetDefaultPrinter() EnumPrinters() $Instructions.Text = "Your printers are listed. Now you can add extra printers and change the default."
While $Form.Visible $nul = Execute($Form.DoEvents) Loop Exit 1
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),'') $Listbox.additem("Default Printer = "+$GetDefaultPrinter) ;Create blank space between default printer listing and the listing of all printers. $Listbox.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 $Listbox.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.&n bsp; ; ;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() SelectAPrinter() Quit() EndFunction
Function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default) ;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: 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()
If $ModPrintersRadioButton.Checked = "True" Select Case ($LibraryPrinterRadioButton.Checked = "False") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.55.72,"Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case ($LibraryPrinterRadioButton.Checked = "True") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.55.72","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case ($ComputerLabRadioButton.Checked = "False") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.55.71","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case ($ComputerLabRadioButton.Checked = "True") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.55.71","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case $OfficePrinterCheckBox.CheckState = -1 AddPrinter("10.48.55.70","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Office Printer",0) EndSelect EndIf EndFunction
Function CheckPrinterRadioButtons() If $DefPrintersRadioButton.Checked = "True" $ComputerLabRadioButton.Enabled = "True" $LibraryPrinterRadioButton.Enabled = "True" $ComputerLabCheckBox.Enabled = "True" $LibraryPrinterCheckBox.Enabled = "True" $OfficePrinterCheckBox.Enabled = "True" $ComputerLabRadioButton.Checked = "True" Else $ComputerLabRadioButton.Enabled = "False" $LibraryPrinterRadioButton.Enabled = "False" $ComputerLabCheckBox.Enabled = "False" $LibraryPrinterCheckBox.Enabled = "False" $OfficePrinterCheckBox.Enabled = "False" $ComputerLabRadioButton.Checked = "False" $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.CheckState = 0 $LibraryPrinterCheckBox.CheckState = 0 $OfficePrinterCheckBox.CheckState = 0 EndIf EndFunction
Function CheckComputerLabRadioButton() $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.Checked = "True" EndFunction
Function CheckLibraryPrinterRadioButton() $ComputerLabRadioButton.Checked = "False" $LibraryPrinterCheckBox.Checked = "True" EndFunction
Function CheckComputerLabCheckBox() If $LibraryPrinterRadioButton.Checked = "True" $ComputerLabCheckBox.Checked = "True" EndIf EndFunction
Function CheckLibraryPrinterCheckBox() If $ComputerLabRadioButton.Checked = "True" $LibraryPrinterCheckBox.Checked = "True" EndIf EndFunction
|
Witto
|
(MM club member)
|
2006-10-05 11:33 PM
|
|
|
|
|
Re: Script to setup printers
|
|
I presume here a quote is missing: Code:
Select Case ($LibraryPrinterRadioButton.Checked = "False") AND ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.55.72","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) Case ($LibraryPrinterRadioButton.Checked = "True") AND ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.55.72","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) Case ($ComputerLabRadioButton.Checked = "False") AND ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.55.71","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) Case ($ComputerLabRadioButton.Checked = "True") AND ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.55.71","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) Case $OfficePrinterCheckBox.CheckState = -1 AddPrinter("10.48.55.70","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Office Printer",0) EndSelect
|
Benny69
|
(MM club member)
|
2006-10-06 01:22 AM
|
|
|
|
|
Re: Script to setup printers
|
|
good catch witto
|
|
Re: Script to setup printers
|
|
Yeah, good catch witto. I made that change. Still had the selectaprinter function has no endfunction error. Then, grasping for straws, I edited further by removing function and endfunction from this section, Function ExitButton() SelectAPrinter() Quit() EndFunction
Then the script worked until I tried to click on the radio button to change printer settings. Then I got a message that the script didn't know what CheckPrinterRadioButtons() was.
Jeremy
|
Benny69
|
(MM club member)
|
2006-10-06 02:40 AM
|
|
|
|
|
Re: Script to setup printers
|
|
try the code like this, do not alter it until after you run it once to see if it is working. Code:
Break On $System = CreateObject("Kixtart.System") If Not $System $nul= MessageBox("KiXforms Not Initiated. This Script Will Now Close.","Error",16) Quit() EndIf
$Form = $System.Form() $Form.Text = "St. Matthew School Computer Network - Admin Site Printer Install" $Form.Width = 600 $Form.Height = 600 $Form.BackColor = $Form.RGB(255,255,0)
$Panel = $Form.Controls.Add("Panel") $Panel.Dock = "top" ;resize for your needs $Panel.BackgroundImage = "\\s096-a0290-01\sch030$\printers\matthew.jpg"
$Instructions = $Form.Controls.Add("Label") $Instructions.Height = 31 $Instructions.Left = 10 $Instructions.TextAlign = 32 ;Center Text in Label $Instructions.Text = "Please wait while I determine which printers you are already connected to..." $Instructions.Top = 135 $Instructions.Width = $Form.ClientWidth-20
$Listbox = $Form.Controls.Add("Listbox") $Listbox.Height = 150 $Listbox.Left = 280 $Listbox.Top = 175 $Listbox.Width = 250
$DefPrintersRadioButton = $Form.Controls.Add("RadioButton") $DefPrintersRadioButton.Left = 10 $DefPrintersRadioButton.Top = $Listbox.Bottom+10 $DefPrintersRadioButton.Text = "Use Current Printer Settings" $DefPrintersRadioButton.Width = 150 $DefPrintersRadioButton.Checked = "True" $DefPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$ModPrintersRadioButton = $Form.Controls.Add("RadioButton") $ModPrintersRadioButton.Left = 10 $ModPrintersRadioButton.Top = $DefPrintersRadioButton.Bottom+10 $ModPrintersRadioButton.Text = "Modify Printer Settings" $ModPrintersRadioButton.Width = 150 $ModPrintersRadioButton.OnClick = "CheckPrinterRadioButtons()"
$PrintersGroupBox = $Form.Controls.Add("GroupBox") $PrintersGroupBox.Left = $ModPrintersRadioButton.Right+10 $PrintersGroupBox.Top = $ModPrintersRadioButton.Top $PrintersGroupBox.Height = 85 $PrintersGroupBox.Width = $Form.ClientWidth-180 $PrintersGroupBox.Text = "Default Install Printer"
$ComputerLabRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $ComputerLabRadioButton.Left = 20 $ComputerLabRadioButton.Top = 20 $ComputerLabRadioButton.Height = 20 $ComputerLabRadioButton.Width = 20 $ComputerLabRadioButton.Enabled = "False" $ComputerLabRadioButton.OnClick = "CheckComputerLabRadioButton()"
$ComputerLabCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $ComputerLabCheckBox.Left = $ComputerLabRadioButton.Right+20 $ComputerLabCheckBox.Top = $ComputerLabRadioButton.Top $ComputerLabCheckBox.Height = 20 $ComputerLabCheckBox.Width = $PrintersGroupBox.Width-$ComputerLabCheckBox.Left-10 $ComputerLabCheckBox.Text = "Computer Lab Printer" $ComputerLabCheckBox.Enabled = "False" $ComputerLabCheckBox.OnClick = "CheckComputerLabCheckBox()"
$LibraryPrinterRadioButton = $PrintersGroupBox.Controls.Add("RadioButton") $LibraryPrinterRadioButton.Left = 20 $LibraryPrinterRadioButton.Top = $ComputerLabRadioButton.Bottom $LibraryPrinterRadioButton.Height = 20 $LibraryPrinterRadioButton.Width = 20 $LibraryPrinterRadioButton.Enabled = "False" $LibraryPrinterRadioButton.OnClick = "CheckLibraryPrinterRadioButton()"
$LibraryPrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $LibraryPrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $LibraryPrinterCheckBox.Top = $LibraryPrinterRadioButton.Top $LibraryPrinterCheckBox.Height = 20 $LibraryPrinterCheckBox.Width = $PrintersGroupBox.Width-$LibraryPrinterCheckBox.Left-10 $LibraryPrinterCheckBox.Text = "Library Printer" $LibraryPrinterCheckBox.Enabled = "False" $LibraryPrinterCheckBox.OnClick = "CheckLibraryPrinterCheckBox()"
$OfficePrinterCheckBox = $PrintersGroupBox.Controls.Add("CheckBox") $OfficePrinterCheckBox.Left = $LibraryPrinterRadioButton.Right+20 $OfficePrinterCheckBox.Top = $LibraryPrinterCheckBox.Bottom $OfficePrinterCheckBox.Height = 20 $OfficePrinterCheckBox.Width = $PrintersGroupBox.Width-$OfficePrinterCheckBox.Left-10 $OfficePrinterCheckBox.Text = "Office Printer" $OfficePrinterCheckBox.Enabled = "False"
$Exit = $Form.Controls.Add("Button") $Exit.Cancel = "True" $Exit.Default = "True" $Exit.Height = 31 $Exit.Left = 10 $Exit.Text = "Click Here To Close This Window" $Exit.Top = $PrintersGroupBox.Bottom+10 $Exit.Width = $Form.ClientWidth-20 $Exit.OnClick = "ExitButton()"
$Form.Center $Form.Show
$nul = GetDefaultPrinter() EnumPrinters() $Instructions.Text = "Your printers are listed. Now you can add extra printers and change the default."
While $Form.Visible $nul = Execute($Form.DoEvents) Loop Exit 1
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),'') $Listbox.additem("Default Printer = "+$GetDefaultPrinter) ;Create blank space between default printer listing and the listing of all printers. $Listbox.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 $Listbox.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.&n bsp; ; ;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() SelectAPrinter() Quit() EndFunction
Function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default) ;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: 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() If $ModPrintersRadioButton.Checked = "True" If ($LibraryPrinterRadioButton.Checked = "False") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.55.72","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",0) EndIf If ($LibraryPrinterRadioButton.Checked = "True") And ($LibraryPrinterCheckBox.CheckState = -1) AddPrinter("10.48.55.72","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Library Printer",1) EndIf If ($ComputerLabRadioButton.Checked = "False") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.55.71","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",0) EndIf If ($ComputerLabRadioButton.Checked = "True") And ($ComputerLabCheckBox.CheckState = -1) AddPrinter("10.48.55.71","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Computer Lab Printer",1) EndIf If $OfficePrinterCheckBox.CheckState = -1 AddPrinter("10.48.55.70","Lexmark T640","\\s096-a290-01\sch030$\printers\t640.inf","Office Printer",0) EndIf EndIf EndFunction
Function CheckPrinterRadioButtons() If $DefPrintersRadioButton.Checked = "True" $ComputerLabRadioButton.Enabled = "True" $LibraryPrinterRadioButton.Enabled = "True" $ComputerLabCheckBox.Enabled = "True" $LibraryPrinterCheckBox.Enabled = "True" $OfficePrinterCheckBox.Enabled = "True" $ComputerLabRadioButton.Checked = "True" Else $ComputerLabRadioButton.Enabled = "False" $LibraryPrinterRadioButton.Enabled = "False" $ComputerLabCheckBox.Enabled = "False" $LibraryPrinterCheckBox.Enabled = "False" $OfficePrinterCheckBox.Enabled = "False" $ComputerLabRadioButton.Checked = "False" $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.CheckState = 0 $LibraryPrinterCheckBox.CheckState = 0 $OfficePrinterCheckBox.CheckState = 0 EndIf EndFunction
Function CheckComputerLabRadioButton() $LibraryPrinterRadioButton.Checked = "False" $ComputerLabCheckBox.Checked = "True" EndFunction
Function CheckLibraryPrinterRadioButton() $ComputerLabRadioButton.Checked = "False" $LibraryPrinterCheckBox.Checked = "True" EndFunction
Function CheckComputerLabCheckBox() If $LibraryPrinterRadioButton.Checked = "True" $ComputerLabCheckBox.Checked = "True" EndIf EndFunction
Function CheckLibraryPrinterCheckBox() If $ComputerLabRadioButton.Checked = "True" $LibraryPrinterCheckBox.Checked = "True" EndIf EndFunction
|
|
Re: Script to setup printers
|
|
Will try it tomorrow morning. Thanks Benny.
|
|
Re: Script to setup printers
|
|
Thanks again to everyone for their help. But I have to risk trying people's patience as now I've decided that it would be nice to add a section to the script that allows users to delete printer(s). I've found two UDFs but they seem to delete all connections. So I made my own function that looks like this so far:
Code:
Function DeleteAPrinter()
Select
Case ComputerLabPrinterRadioButton.Checked = "True" DelPrinterConnection ComputerLabPrinter Case LibraryPrinterRadioButton.Checked = "True" DelPrinterConnection LibraryPrinter Case OfficePrinterRadioButton.Checked = "True" DeletePrinterConnection OfficePrinter
;If Printer Button is check off, but printer is not currently installed, then ? That printer is not currently installed on your computer.
;If no Radio Button is checked off, then ? You chose not to delete a printer. Existing Script.
EndSelect EndFunction
I thought that it would be easiest to create a 'checkboxable' list of all possible printers on the system rather then trying to create a checkbox list from all the enumerated printers (cause I thought that would be tougher to do). Then I could set it up so that users could choose one or more printers to delete in one go. I tried to follow the other bits of code in the beginning of the script to do this but I'm getting a bit confused. Tomorrow I'll look on the KixForms Online Help page and that will probably give me the answer.
But could someone tell me if my process for deleting printers seems logical? And I'm assuming that it's not bad practice or a security risk to delete the printer connections but not necesarily the printers drivers and ports? And one last question. I'd probably never do this, but does putting a space in a printer share (i.e. "\\server\lexmark printer") cause issues? (Shares are on a W2K3 server and all clients are either XP or 2K).
Thanks again for everyone's support. Jeremy
|
Benny69
|
(MM club member)
|
2006-10-06 02:29 PM
|
|
|
|
|
Re: Script to setup printers
|
|
I would suggest creating a 'Test User' to test your logic.
|
|
Re: Script to setup printers
|
|
I will test it out on a test user. (Yes, I can do some work by myself )
The form comes up without any errors now. Sorry, but I have one more dumb question cause there is one thing I can't fiugre out. Am I supposed to see a button that says Apply Changes? Or are changes supposed to apply once I click on the Close this Window button?
Thanks, Jeremy
|
Benny69
|
(MM club member)
|
2006-10-06 07:00 PM
|
|
|
|
|
Re: Script to setup printers
|
|
Quote:
Click the ‘Click Here To Close This Window’ and the chosen printer settings will be applied.
|