Jump to content
Sign in to follow this  
dot45

Windows XP & 2000Pro Printer Backup/migration script

Recommended Posts

dot45

I'm sharing my printer migration script now that its all polished up.

If you see any changes that would make it work better, please let me know.

Feel free to use this script, but please give credit.

edited 7/08/2009: updated code to fix a few things. Delete function doesnt work with win2k (added msgbox reminding user to delete manually.)

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=H:\icons\computer-icons\printer2.ico
#AutoIt3Wrapper_outfile=..\print_migration_v6-beta.exe
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;This is a printer migration script for backing up/deleting/restoring network & local printers
;It its current form the "Printmig.exe" (Microsofts printer migration tool v3.1) and "printmngr.vbs (from an XP installation)
;are copied to a network share for the script to work from. This same share is also used to store the CAB files in a central location.
;
;
;
;Written by Chris Catlett (dot45 on autoIT forums)
Dim $RunOnce, $SystemRoot, $Message, $ButtonPressed, $PID
; Get the SystemRoot variable from the computer environment to be used later in the script
$SystemRoot = EnvGet("SYSTEMROOT")
;Get the WindowsDir variable to be used later in the script
$WindowsDir = @WindowsDir
$OStat = ""</P> <P>$Form2 = GUICreate("Printer tool", 195, 192, 193, 125)
$AButton1 = GUICtrlCreateButton("Local Storage", 32, 24, 121, 57, 0)
$AButton2 = GUICtrlCreateButton("Network Storage", 32, 96, 121, 65, 0)
GUISetState(@SW_SHOW)</P> <P>While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $AButton1
   $OStat = "1"
   ExitLoop
  Case $AButton2
   $OStat = "2"
   ExitLoop
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
WEnd</P> <P>
IF $OStat = "2" Then
;checking if Z: is currently in use
$PMap = DriveMapGet ("Z:")
if @error = "1" Then
MsgBox(0,"No mapping detected","No drive mapping detected on Z:",1)
$PMap = "NULL"
Else
MsgBox(0,"A mapping was detected on Z:",$PMap,1)
EndIf
;this maps the shared drive where the printmig.exe and printmngr.vbs files sit.
MsgBox(0,"Removing Existing Mapping",$PMap,1)
DriveMapDel ("Z:")
MsgBox(0,"Mapping Printer Migration Location","<A href='file://\\server\Shared\PMig",1'>\\server\Shared\PMig",1</A>)
DriveMapAdd("Z:","<A href="file://\\<IP">\\<IP</A> ADDRESS>\Shared\PMig",0,"domain\user","password")
;checking that the mapping worked correctly
$folder="Z:\printer\printmig.exe"
IF FileExists($folder) Then
msgbox (0,"Printer tool Found","Proper files found on the network",1)
Else
msgbox (0,"Printer tool not found","Please validate network connectivity - printer tool not detected",1)
exit
EndIf</P> <P>; *******************************************************************
; ****************** Prompted Menu **********************************
; *******************************************************************</P> <P>$Form1 = GUICreate("Printer Migration Tool - v5", 299, 317, 280, 137)
$BButton1 = GUICtrlCreateButton("Backup Printer Settings", 57, 16, 193, 65, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$BButton2 = GUICtrlCreateButton("Delete Printer Settings", 57, 88, 193, 65, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$BButton3 = GUICtrlCreateButton("Restore Printer Settings", 57, 160, 193, 65, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$BButton4 = GUICtrlCreateButton("Exit", 105, 240, 113, 49, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
GUISetState(@SW_SHOW)</P> <P>
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $BButton1
   BKUPPR()
  Case $BButton2
   DELPR()
  Case $BButton3
   RSTPR()
  Case $BButton4
   ExitLoop
  Case $GUI_EVENT_CLOSE
EndSwitch
WEnd</P> <P>
DriveMapDel ("Z:")
;this will remove the drive we set and restore if it was in use.
if $PMap = "NULL" Then
MsgBox(0,"No mappings to restore","Null drive mapping on Z:",1)
Else
MsgBox(0,"Restoring Previous Mapping",$PMap,1)
DriveMapAdd("Z:",$PMap)
EndIf</P> <P>
EndIf</P> <P>; *******************************************************************
; *******************************************************************
; *******************************************************************
; *******************************************************************
; *******************************************************************
; *******************************************************************</P> <P>
IF $OStat = "1" Then</P> <P>; *******************************************************************
; ****************** Prompted Menu **********************************
; *******************************************************************</P> <P>;copying the files down to the local machine if not running the network based backup.
FileInstall("Z:\printer\printmig.exe",@WorkingDir,1)
FileInstall("Z:\printer\prnmngr.vbs",@WorkingDir,1)</P> <P>
$Form1 = GUICreate("Printer Migration Tool - v5", 299, 317, 280, 137)
$CButton1 = GUICtrlCreateButton("Backup Printer Settings", 57, 16, 193, 65, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$CButton2 = GUICtrlCreateButton("Delete Printer Settings", 57, 88, 193, 65, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$CButton3 = GUICtrlCreateButton("Restore Printer Settings", 57, 160, 193, 65, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$CButton4 = GUICtrlCreateButton("Exit", 105, 240, 113, 49, 0)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
GUISetState(@SW_SHOW)</P> <P>
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $CButton1
   LBKUPPR()
  Case $CButton2
   LDELPR()
  Case $CButton3
   LRSTPR()
  Case $CButton4
   ExitLoop
  Case $GUI_EVENT_CLOSE
EndSwitch
WEnd</P> <P>
EndIf</P> <P> </P> <P>Func BKUPPR()
$BLD = ""</P> <P> While $BLD = ""
  $BLD = InputBox ("Enter Building or Department", "Enter a file descriptor for the backup file. The computer name already appended by the tool")
   If @error = 1 Then
MsgBox(0, 'Error', 'You must enter a file description to avoid conflict')
   EndIf
Wend</P> <P>MsgBox(0, "Saving Current printer settings","Backing up current configuration to the network",1)</P> <P>
RunWait(@COMSPEC & " /c $WindowsDir\System32\cscript.exe Z:\printer\prnmngr.vbs -l > Z:\"& $BLD &"_%computername%.txt")</P> <P>RunWait(@COMSPEC & " /c Z:\printer\printmig.exe -b Z:\"& $BLD &"_%computername%.cab")</P> <P>MsgBox (0,"Printer settings backed up"," File Name: Z:\"& $BLD & "_"& @computername &".cab")</P> <P>EndFunc</P> <P> </P> <P>Func DELPR()
MsgBox(0,"Removing all Printers","Please wait - Uninstalling local printers",1)</P> <P> If @OSVersion = "win_2000" Then
  MsgBox(0,"Windows 2000 is not supported", "You must manually remove the existing printers from this computer.", 3)
Else
  RunWait(@COMSPEC & " /c $WindowsDir\System32\cscript.exe Z:\printer\prnmngr.vbs -x")</P> <P>  RunWait(@COMSPEC & " /c net stop LPDSVC")
  RunWait(@COMSPEC & " /c net stop spooler")
  RunWait(@COMSPEC & " /c net start spooler")
  RunWait(@COMSPEC & " /c net start LPDSVC")</P> <P> MsgBox(0,"Removal Completed","All printers removed",1)
EndIf</P> <P>
EndFunc</P> <P>
Func RSTPR()
$message = "Select the Printer Restore CAB to begin"
$RSTFILE = ""</P> <P> While $RSTFILE = ""
  MsgBox (1,"Please Select your restore file","Please Select a file to continue or click Cancel to exit")
  $RSTFILE = FileOpenDialog($message, "Z:","CAB(*.cab)", 1 + 4 )
   If @error = 1 Then
MsgBox(0, '', 'The Cancel button was pushed')
   Exit
   EndIf
Wend</P> <P> MsgBox (0,"File Selected", $RSTFILE,1)</P> <P> MsgBox(0,"Installing New Printer Settings","New printer configuration being installed",1)</P> <P> RunWait(@COMSPEC & " /c Z:\printer\printmig.exe -r "&  $RSTFILE)</P> <P>RunWait(@COMSPEC & " /c net stop LPDSVC")
RunWait(@COMSPEC & " /c net stop spooler")
RunWait(@COMSPEC & " /c net start spooler")
RunWait(@COMSPEC & " /c net start LPDSVC")</P> <P>MsgBox(0,"Migration Completed","You may need to reboot to see the new printers",3)
EndFunc</P> <P>Func LBKUPPR()</P> <P>RunWait(@COMSPEC & " /c $WindowsDir\System32\cscript.exe" & @WorkingDir & "\prnmngr.vbs -l > c:\"& @computername &".txt")</P> <P>
RunWait(@COMSPEC & " /c " & @WorkingDir & "\printmig.exe -b c:\"& @computername & ".cab")</P> <P>MsgBox (0,"Printer settings backed up"," File Name: C:\"& @computername &".cab")</P> <P>EndFunc</P> <P>
Func LDELPR()
MsgBox(0,"Removing all Printers","Please wait - Uninstalling local printers",1)</P> <P> If @OSVersion = "win_2000" Then
  MsgBox(0,"Windows 2000 is not supported", "You must manually remove the existing printers from this computer.", 3)
Else
  RunWait(@COMSPEC & " /c $WindowsDir\System32\cscript.exe " & @WorkingDir & "\prnmngr.vbs -x")
  RunWait(@COMSPEC & " /c net stop LPDSVC")
  RunWait(@COMSPEC & " /c net stop spooler")
  RunWait(@COMSPEC & " /c net start spooler")
  RunWait(@COMSPEC & " /c net start LPDSVC")</P> <P>  MsgBox(0,"Removal Completed","All printers removed",1)
EndIf</P> <P>
EndFunc</P> <P>
Func LRSTPR()
$message = "Select the Printer Restore CAB to begin"
$RSTFILE = ""</P> <P> While $RSTFILE = ""
  MsgBox (1,"Please Select your restore file","Please Select a file to continue or click Cancel to exit")
  $RSTFILE = FileOpenDialog($message, "c:","CAB(*.cab)", 1 + 4 )
   If @error = 1 Then
MsgBox(0, '', 'The Cancel button was pushed')
   Exit
   EndIf
Wend</P> <P> MsgBox (0,"File Selected", $RSTFILE,1)</P> <P> MsgBox(0,"Installing New Printer Settings","New printer configuration being installed",1)</P> <P> RunWait(@COMSPEC & " /c " & @WorkingDir & "\printmig.exe -r "&  $RSTFILE)</P> <P>RunWait(@COMSPEC & " /c net stop LPDSVC")
RunWait(@COMSPEC & " /c net stop spooler")
RunWait(@COMSPEC & " /c net start spooler")
RunWait(@COMSPEC & " /c net start LPDSVC")</P> <P>MsgBox(0,"Migration Completed","You may need to reboot to see the new printers",3)
EndFunc

printer2.ico

print_migration_v7.au3

Edited by dot45

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×