Jump to content

Migrating Print Servers

Recommended Posts


So, this is not one of my neatest scripts, but I haven't contributed much to the forum lately, and this was a help to me this week.

I was migrating about 40 print queues from an old print server to a new one.  Since this entails removing the old printer and creating the new printer for each user on each PC for each print queue used (for about 350 users), doing this manually was out of the question.

I just added this to the logon scripts to run once per user per machine.

If anyone finds the need to make a similar migration, please feel free to use/modify this.

Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ;don't want to crash out if there is a problem.

#include "PrintMgr.au3" ;https://www.autoitscript.com/forum/topic/155485-printers-management-udf/
#include "ServiceControl.au3" ;https://www.autoitscript.com/forum/topic/6487-updated-service-control-funtions/
#include <array.au3>

Global $sOldPrintServer = "oldservername"
Global $sNewPrintServer = "newservername"

Global $sDefaultPrinter = _CleanName(_GetDefaultPrinter())
;If you change print queue names below, you may need to take that into account and modify $sDefaultPrinter as well.

Global $aPrinters = _PrintMgr_EnumPrinter()
If @error Then Exit
Global $aReplaced[1] = [0]
Global $sTempName

;remove printers that are mapped to old server
For $iLoop = 1 To $aPrinters[0]
    If Not StringInStr($aPrinters[$iLoop], $sOldPrintServer) Then ContinueLoop ;not a printer we are concerned with
    $sTempName = $aPrinters[$iLoop]
    ConsoleWrite("Removing " & $sTempName & @CRLF)
    $sTempName = _CleanName($sTempName)

    ;If you need to change a print queue name, modify $sTempName here before adding it to the array.

    _ArrayAdd($aReplaced, $sTempName)
    $aReplaced[0] = $aReplaced[0] + 1

;Stop and re-start print spooler.  This alleviated problems on a few machines.
ConsoleWrite("Stopping Spooler" & @CRLF)
_StopService(@ComputerName, "spooler")
ConsoleWrite("Starting Spooler" & @CRLF)
_StartService(@ComputerName, "spooler")

;remap to the new server (assuming all of the print queues have the same share name)
For $iLoop = 1 To $aReplaced[0]
    Local $isdefault = False
    If $aReplaced[$iLoop] = $sDefaultPrinter Then $isdefault = True
    ConsoleWrite("Adding \\" & $sNewPrintServer & "\" & $aReplaced[$iLoop] & "(" & $isdefault & ")" & @CRLF)
    _PrintMgr_AddWindowsPrinterConnection("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop])
    If $isdefault Then _PrintMgr_SetDefaultPrinter("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop])

Func _GetDefaultPrinter() ; CyberSlug - 18 Nov 2004
    Local $key, $default, $defPrtNm
    If @OSType = "WIN32_WINDOWS" Then
        $key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print\Printers"
        $defPrtNm = RegRead("HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers", "Default")
    Else ;WIN_NT type
        $key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers"
        $default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device")
        $defPrtNm = StringLeft($default, StringInStr($default, ",") - 1)
    Return $defPrtNm
EndFunc   ;==>_GetDefaultPrinter

Func _CleanName($sName)
    Local $sTempName = $sName
    $sTempName = StringReplace($sTempName, "\\" & $sOldPrintServer & "\", "")
    Return $sTempName
EndFunc   ;==>_CleanName

Func MyErrFunc()
    ;just dump the info to the console and keep going
    Local $sErrNum
    Local $sMsg
    $sErrNum = Hex($oMyError.Number, 8)
    $sMsg = "Error Number: " & $sErrNum & @CRLF
    $sMsg &= "WinDescription: " & $oMyError.WinDescription & @CRLF
    $sMsg &= "Script Line: " & $oMyError.ScriptLine & @CRLF
EndFunc   ;==>MyErrFunc


Edited by willichan
Updated script to remove redundant function
  • Like 1

Share this post

Link to post
Share on other sites
3 hours ago, jguinch said:

your _MapPrinter was the equivalent of _PrintMgr_AddWindowsPrinterConnection in the UDF

Your UDF was a godsend. 

I guess I didn't scan far enough down the list of functions to see _PrintMgr_AddWindowsPrinterConnection.  I stopped scanning at _PrintMgr_AddPrinter and just used the function from an old script.

I will update the script to just use the UDF functions.

Thank you for the UDF.

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

  • Similar Content

    • jguinch
      By jguinch
      I did create these few functions several months ago. I post here, if it can interest someone.
      These functions based on WMI queries allow you to manage printers : add / delete printer, driver, port, or obtain configuration, set default printer ... I let you discover it with the code.

      Here is the list of the available functions :
      And some examples :
      #Include "PrintMgr.au3" ; Remove a printer called "My old Lexmark printer" : _PrintMgr_RemovePrinter("My old Lexmark printer") ; Remove the driver called "Lexmark T640" : _PrintMgr_RemovePrinterDriver("Lexmark T640") ; Remove the TCP/IP printer port called "TCP/IP" _PrintMgr_RemoveTCPIPPrinterPort("MyOLDPrinterPort") ; Add a driver, called "Samsung ML-451x 501x Series", and driver inf file is ".\Samsung5010\sse2m.inf" _PrintMgr_AddPrinterDriver("Samsung ML-451x 501x Series", "Windows NT x86", @scriptDir & "\Samsung5010", @scriptDir & "\Samsung5010\sse2m.inf") ; Add a TCP/IP printer port, called "MyTCPIPPrinterPort", with IPAddress = and Port = 9100 _PrintMgr_AddTCPIPPrinterPort("MyTCPIPPrinterPort", "", 9100) ; Add a printer, give it the name "My Printer", use the driver called "Samsung ML-451x 501x Series" and the port called "MyTCPIPPrinterPort" _PrintMgr_AddPrinter("My Printer", "Samsung ML-451x 501x Series", "MyTCPIPPrinterPort") ; Set the printer called "My Printer" as default printer _PrintMgr_SetDefaultPrinter("My Printer") ; Connect to the shared printer "\\\HPDeskjetColor") _PrintMgr_AddWindowsPrinterConnection("\\\HPDeskjetColor") ; List all installed printers #Include <Array.au3> $aPrinterList = _PrintMgr_EnumPrinter() _ArrayDisplay($aPrinterList) ; List all printers configuration #Include <Array.au3> $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration() _ArrayDisplay($aPrinterConfig) ; List all installed printer drivers #Include <Array.au3> $aDriverList = _EnumPrinterDriver() _ArrayDisplay($aDriverList) ; Retrieve the printer configuration for the printer called "Lexmark T640" #Include <Array.au3> $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration("Lexmark T640") _ArrayDisplay($aPrinterConfig) ; Add a local printer port (for a file output) _AddLocalPrinterPort("c:\temp\output.pcl") ; Remove the local port _RemoveLocalPrinterPort("c:\temp\output.pcl") Download link :  PrintMgr.au3