Page 2 of 2 <12
Topic Options
#168687 - 2006-10-06 01:55 AM Re: Script to setup printers
jeremyschubert Offline
Getting the hang of it

Registered: 2005-09-17
Posts: 89
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

Top
#168688 - 2006-10-06 02:40 AM Re: Script to setup printers
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
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

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#168689 - 2006-10-06 03:58 AM Re: Script to setup printers
jeremyschubert Offline
Getting the hang of it

Registered: 2005-09-17
Posts: 89
Will try it tomorrow morning.
Thanks Benny.

Top
#168690 - 2006-10-06 06:41 AM Re: Script to setup printers
jeremyschubert Offline
Getting the hang of it

Registered: 2005-09-17
Posts: 89
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
_________________________
---
Bishop Grandin Technology Department
'Either we're on time, or we're late'

Top
#168691 - 2006-10-06 02:29 PM Re: Script to setup printers
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
I would suggest creating a 'Test User' to test your logic.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#168692 - 2006-10-06 06:40 PM Re: Script to setup printers
jeremyschubert Offline
Getting the hang of it

Registered: 2005-09-17
Posts: 89
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

Top
#168693 - 2006-10-06 07:00 PM Re: Script to setup printers
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Quote:


Click the ‘Click Here To Close This Window’ and the chosen printer settings will be applied.



_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
Page 2 of 2 <12


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 765 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.058 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org