; PrintMigrate - Migrates printers from one server to another
; Glenn Barnas - 2006/04/14
;
; Enumerates all printers, moving printers mapped on the old server to the new server
; assumes that old and new printer names are the same. The default printer is preserved.
; Only one print server (per pass) can be migrated with this utility
;
; The old and new print servers must be defined below
;
; If the old and new printer names are not the same, or printers are being moved between
; multiple print servers, use PrintMigrate2, which utilizes a lookup table to map the old
; server\printer to new server\printer. PrintMigrate2 utilizes a similar process to
; PrintMigrate, but is slower due to the 1:1 mapping needed.
Break On
Dim $
Dim $OldPServer, $NewPServer ; old/new print server names
Dim $LPKey ; Key where printers are defined
Dim $DefPtr ; UNC of default printer
Dim $Value ; value read from registry
Dim $I ; index pointer
Dim $Tag ; flag indicating an error in delete/map process
$ = SetOption('Explicit', 'On')
$ = SetOption('WrapAtEOL', 'On')
$ = SetOption('NoVarsInStrings', 'On')
$ = SetOption('NoMacrosInStrings', 'On')
;##############################################################
; Define old & new print servers
$OldPSvr = '\\cmi-25'
$NewPSvr = '\\nsfcmi-4'
;##############################################################
; define key where printers are defined
$LPKey = 'HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts'
; Get current default printer
$DefPtr = ReadValue('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows', 'Device')
; Display the default printer
If $DefPtr
'Default printer is ' Split($DefPtr, ',')[0] ? ?
Else
'Default printer is not defined.' ? ?
EndIf
; Enumerate the printers defined for this user
$I = 0 ; set the index to 0
; get the first registry value
$Value = EnumValue($LPKey, $I)
While Not @ERROR ; process until an EOD error
'Checking ' $Value
$Tag = 0 ; clear the error tag
If InStr($Value, $OldPSvr) ; If it references the old server
$Printer = Join(Split($Value, $OldPSvr), '') ; get the printer name
DelPrinterConnection($Value) ; delete the current printer connection
If Not @ERROR ; handle error
' - deleted'
Else
' Failed to delete printer!' ?
$Tag = 1
EndIf
If Not $Tag ; add if delete was successful
AddPrinterConnection($NewPSvr + $Printer)
If Not @ERROR
', added'
Else
' Failed to add printer!' ?
$Tag = 1
EndIf
If Not $Tag
If InStr($DefPtr, $Printer)
SetDefaultPrinter($NewPSvr + $Printer)
If Not @ERROR
', Set as default'
Else
' Failed to set as default printer!' ?
EndIf
EndIf
EndIf
?
EndIf
Else
' - OK' ?
EndIf
$I = $I + 1
$Value = EnumValue($LPKey, $I)
Loop