Page 1 of 1 1
Topic Options
#196628 - 2009-11-10 04:12 PM Check for installed printers
FerrisBueller Offline
Just in Town

Registered: 2009-11-10
Posts: 2
Loc: Germany
Hi all,

I'm new to this forum and I hope I picked the right board for my request.
We're using kix for quite some time now and it works great. Kix is adding printers, network drives, starts some batch files. Pretty simple stuff...
Now we're planning to migrate our print server. Due to the fact the users are able to connect to network printers by them selves, I'm looking for a way to let kix check if there's a printer installed or not on a local machine.
Something like 'if printer "\\SERVER01\PrinterA" is installed, then remove it an add "\\SERVER02\PRINTERA"'. How would you guys accomplish that?

Thanx for any replies.

Top
#196629 - 2009-11-10 04:47 PM Re: Check for installed printers [Re: FerrisBueller]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Hi and welcome to the board. New to this forum and I’m going to dump you in deep water right away :-) because that is the easiest way to learn imho.

The code below gets all current printers, checks for OLDSERVERNAME and disconnects any old printers, replaces OLDSERVERNAME with NEWSERVERNAME and connects the new printers.

It is not that difficult so have a look at the code below and let us know if you have any questions.

CAUTION: the code below is untested.
 Code:
Break on

;Enumerate all current printers using the EnumPrinterConnection2 UDF.
$oldprinters = EnumPrinterConnections2()

;Do stuff for each old printer
For Each $oldprinter in $oldprinters
	;Split the printer on the comma to get only the printername.
	$oldprinter = Split($poldrinter, ",")
	;Check if the old servername is in the printername.
	If InStr($oldprinter[0], "OLDSERVERNAME")
		;Delete the printer if the old servername is found in the name.
		$rc = DelPrinterConnection($oldprinter)
		;Replace oldservername with newservername.
		$newprinter = REPLACE ($oldprinter, "OLDSERVERNAME", "NEWSERVERNAME")
		;Add the new printer using the newservername.
		$rc = AddPrinterConnection($newprinter)
	EndIf
Next

Sleep 5


;BELOW IS A UDF. DO NOT MODIFY ANYTHING IN THE UDF, IT COMES READY FOR USE.
;
;Function		EnumPrinterConnections2() - Enumerates all connected printers 
;   
;Author			NTDOC 
;   
;Contributors		Bryce 
;   
;Action			Enumerates all of the installed / connected printers into an array  
;   
;Syntax			EnumPrinterConnections2()  
;   
;Version		1.0  
;   
;Date			2005-Apr-20   
;   
;Date Revised		xxxx-xxx-xx   
;   
;Revision Reason	  
;   
;Parameters		None 
;   
;Remarks		Based on the EnumPrinterConnections() UDF 
;			http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=83545 
;			Which I believe was probably derived from here at Microsoft EnumPrinterConnections Method 
;			Minor modifications due to invalid array dim and array starts 
;			with a blank which can cause issues when trying to display or 
;			write to a log file.  This method should work a little better. 
;			 
;			Tested on 2000/XP/2003  
;   
;Returns		An array of all the installed / connected printers 
;   
;Dependencies		KiXtart v4.x, WSH 5.6  
;			Written and tested with KiXtart v4.23 
; 
;Example		NOTE! This example uses the QS UDF to sort the list 
;			http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82876 
; 
;			Dim $PrinterData, $PrinterMap, $Pi 
;			$PrinterData = QS(EnumPrinterConnections2()) 
;			If Ubound($PrinterData) <> -1 
;			  For $Pi = 0 To Ubound($PrinterData) 
;			    $PrinterMap = Split($PrinterData[$Pi],",",-1) 
;			    $PrinterMap[0]+CHR(9)+$PrinterMap[1] ? 
;			  Next 
;			Else 
;			  ? 'No printer mappings found.' 
;			EndIf 
; 
;Source  
Function EnumPrinterConnections2()
  Dim $WshNetwork, $oPrinters, $i, $PrintArray[0]
  $EnumPrinterConnections2=""
  $WshNetwork = CreateObject("WScript.Network")
  If Not @ERROR
    $oPrinters = $WshNetwork.EnumPrinterConnections
    For $i = 0 to $oPrinters.Count - 1 Step 2
      $PrintArray[UBound($PrintArray)]=$oPrinters.Item($i+1)+','+$oPrinters.Item($i)
      ReDim Preserve $PrintArray[UBound($PrintArray)+1]
    Next
      If UBound($PrintArray) > 0
        ReDim Preserve $PrintArray[UBound($PrintArray)-1]
      Else
        $PrintArray = 0
      EndIf
  Else
    $EnumPrinterConnections2=@ERROR
    Exit $EnumPrinterConnections2
  EndIf
  $EnumPrinterConnections2=$PrintArray
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#196631 - 2009-11-10 05:14 PM Re: Check for installed printers [Re: Mart]
FerrisBueller Offline
Just in Town

Registered: 2009-11-10
Posts: 2
Loc: Germany
Hi Mart,

well that was a quick answer. :-) Thanks. I'll check it in the next days and let you know, if it worked.

_______________
Ferris.

Top
#196635 - 2009-11-10 08:38 PM Re: Check for installed printers [Re: FerrisBueller]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Have fun!
I used the Replace() function. This function is first introduced in kixtart 4.61 (the latest release) so be sure to use the correct version of kixtart.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#196636 - 2009-11-10 08:46 PM Re: Check for installed printers [Re: FerrisBueller]
DemoDog Offline
Fresh Scripter

Registered: 2009-08-20
Posts: 9
Loc: Washington DC
This works real good, shamelessly copied it from 'Start to Finish Guide to Scripting with Kixtart' by Bob Kelly , worth every penny
DemoDog

 Code:
$NewServer="\\new\"
$OldServer="\\old\"
$Cnt=0
$SubKey= "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Printerports"
$PrintName=EnumValue ($SubKey, $Cnt)
While $PrintName <> ""
       If Left($PrintName, Len($OldServer))=$OldServer
               $NewPrintName=$NewServer + SubStr($PrintName,Len($OldServer) + 1,Len($PrintName))
        ? "Deleting; " $PrintName "..."
        $= DelPrinterConnection("$PrintName")
        ? @SERROR
        ? "Connecting; " $NewPrintName "..."
        $= AddPrinterConnection("$NewPrintName")
        ? @SERROR
Else
        $Cnt= $Cnt + 1
Endif
$PrintName=EnumValue ($Subkey, $Cnt)

Loop


Edited by Mart (2009-11-10 09:39 PM)
Edit Reason: Please use code tags when posting code.
_________________________
If Stupidity got us into this mess, then why can't it get us out?
Will Rogers

Top
Page 1 of 1 1


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

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

Generated in 0.054 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

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