Break on
$printers = EnumPrinterConnections2()
$defaultprinter = GetDefaultPrinter()
$oldserver = "oldservername"
$newserver = "newservername"
For Each $printer in $printers
If InStr($printer, $oldserver)
$rc = DelPrinterConnection($printer)
$newprinter = Split($printer, $oldserver)
$newprinter = Join($newprinter, $newserver")
$rc = AddPrinterConnection($newprinter)
If $printer = $defaultprinter
$rc = SetDefaultPrinter($newprinter)
EndIf
EndIf
Next
;=========================================================
;===== DOT NOT MODIFY ANYTHING BELOW THIS LINE ===========
;===== THIS ARE UDF"S AND THEY COME READY MADE ===========
;=========================================================
;FUNCTION GetDefaultPrinter()
;
;AUTHOR Jochen Polster
;
;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
Function GetDefaultPrinter()
$GetDefaultPrinter = Join(Split(ReadValue("HKU\" + @sid + "\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device"), ',', 1), '')
EndFunction
;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