code:
; rem
:Change_Printer
; rem #################################################################
; rem ## Emergency printer port change for when server is down. ##
; rem ## Process is dynamic, no systems will require reboot. ##
; rem ## ##
; rem ## Process built as 'hot' cutover to print server with ##
; rem ## defined printers AND shares named EXACTLY as primary ##
; rem ## server, and printers already configured on client. ##
; rem ## ##
; rem ## !! DOES NOT CONFIRM PRINTER DRIVERS INSTALLED, ETC. !! ##
; rem ## ##
; rem #################################################################
; REM ** Variable in use by this script
DIM $Alpha, $Count, $CurPrn, $CurSvr, $LenPrn, $LenSvr, $NewPort, $NewPrnKey, $NewSvr
DIM $OldPort, $OldPrnKey, $OldSvr, $RegKey
$OldSvr = "XYZ987" $NewSvr = "ABC123" $Alpha = "Backward"
; $OldSvr = "ABC123" $NewSvr = "XYZ987" $Alpha = "Forward"
SELECT
CASE (@INWIN = 1)
; REM ** Index tracker - increments or decrements depending upon alpha-numeric naming of servers.
; REM ** Failure to decrement will mean some print devices skipped as index values change with rename process.
$HKEY = ("HKEY_CURRENT_USER\Printers\Connections")
SELECT
CASE ($Alpha = "Forward")
$Count = -1
WHILE (@ERROR = 0)
$Count = $Count + 1
$OldPrnKey = ENUMKEY ($HKEY, $Count)
LOOP
$x1 = ($Count-1)
CASE (1) $x1 = 0
ENDSELECT
; REM ** Find the printer key, check if matches 'current' printer, and the values associated with the key.
; REM ** After finding values of the key, delete old key and write new key and values if do not already exist.
:LOOP_NT_Printer
$OldPrnKey = ENUMKEY ($HKEY, $x1)
IF (@ERROR = 0)
; REM ** Parse port name and printer name to variables
$LenPrn = LEN ($OldPrnKey)
$LenSvr = LEN ($OldSvr)
$CurPrn = SUBSTR ($OldPrnKey, (($LenSvr)+4), $LenPrn)
$CurSvr = SUBSTR ($OldPrnKey, 3, ($LenPrn - ((LEN ($CurPrn) +3 ))))
$RegKey = ($HKEY + "\" + $OldPrnKey)
IF ($CurSvr <> $NewSvr)
DIM $RegVal[20] ; REM ** Variable count of 20 printers arbitrary - my system only had 3 keys.
DIM $RegSet[20]
DIM $RegTyp[20]
$x2 = 0 ; REM ** Index to loop through value entries for each subkey/printer
:LOOP_PrnVal
$RegVal[$x2] = ENUMVALUE ($RegKey, $x2)
SELECT
CASE (@ERROR = 0)
$RegSet[$x2] = READVALUE ($RegKey, $RegVal[$x2])
$RegTyp[$x2] = READTYPE ($RegKey, $RegVal[$x2])
IF ($RegSet[$x2] = "\\" + $OldSvr) $RegSet[$x2] = "\\" + $NewSvr ENDIF
$x2 = $x2+1
GOTO LOOP_PrnVal
CASE (1)
DELKEY ($RegKey)
IF (@ERROR = 0) $Mgg = "Success"
ELSE $Msg = "Error @ERROR"
ENDIF
$wri = WRITELINE (1, "$Msg deleting Printer registry key: $RegKey" + $CR)
$NewPrnKey = ($HKEY + "\,," + $NewSvr + "," + $CurPrn)
$RC = EXISTKEY ($NewPrnKey) ; REM ** Only make key if does not exist
IF (@ERROR = 0)
$wri = WRITELINE (1, "Printer registry key already exists: $NewPrnKey" + $CR)
ELSE
ADDKEY ($NewPrnKey)
IF (@ERROR = 0) $Msg = "Success"
ELSE $Err = $Err+1 $Msg = "Error @ERROR"
ENDIF
$wri = WRITELINE (1, "$Msg adding Printer registry key: $NewPrnKey" + $CR)
$x3 = 0 ; REM ** Index to match with value entry count from above
WHILE ($x3 < $x2)
$wri = WRITEVALUE ("$NewPrnKey", $RegVal[$x3], $RegSet[$x3], $RegTyp[$x3])
IF (@ERROR = 0) $x3 = $x3+1 $Msg = "Success"
ELSE $Err = $Err+1 $Msg = "Error @ERROR"
ENDIF
$wri = WRITELINE (1, "$Msg writing Printer value: $NewPrnKey, " + $RegSet[$x3] + $CR)
LOOP
ENDIF
ENDSELECT
ENDIF
SELECT
CASE ($Alpha = "Forward") $x1 = $x1-1
CASE (1) $x1 = $x1+1
ENDSELECT
GOTO LOOP_NT_Printer
ENDIF
CASE (1)
$HKEY = ("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers")
$x1 = 0
:LOOP_Win9X_Printer
$OldPrnKey = ENUMKEY ($HKEY, $x1)
$RegKey = ($HKEY + "\" + $OldPrnKey)
$OldPort = READVALUE ($RegKey, "Port")
IF (@ERROR = 0)
$LenPrn = LEN ($OldPort)
$LenSvr = LEN ($OldSvr)
$CurPrn = SUBSTR ($OldPort, (($LenSvr)+4), $LenPrn)
$CurSvr = SUBSTR ($OldPort, 3, ($LenPrn - ((LEN ($CurPrn) +3 ))))
$NewPort = "\\" + $NewSvr + "\" + $CurPrn
IF ($CurSvr = $NewSvr)
$wri = WRITELINE (1, "Printer registry key already exists: $NewPrnKey" + $CR)
ELSE
$wri = WRITEVALUE ("$RegKey", "Port", $NewPort, "REG_SZ")
IF (@ERROR = 0) $Msg = "Success"
ELSE $Err = $Err+1 $Msg = "Error @ERROR"
ENDIF
$wri = WRITELINE (1, "$Msg adding Print registry key: $NewPrnKey, $NewPort" + $CR)
ENDIF
$x1 = $x1+1
GOTO LOOP_Win9X_Printer
ENDIF
ENDSELECT
RETURN