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