Jump to content

Migrating Print Servers


willichan
 Share

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)
    _PrintMgr_RemovePrinter($sTempName)
    $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
Next

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

;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])
Next

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)
    EndIf
    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
    ConsoleWrite($sMsg)
EndFunc   ;==>MyErrFunc

 

Edited by willichan
Updated script to remove redundant function
Link to comment
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.

Link to comment
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
 Share

×
×
  • Create New...