Renaming, Changing, or Moving Print Queues
Posted by Daniel on 02/12/2010Renaming printer queues on a Windows print server is easy. Getting all the client machines on your network to switch over to the new queue name is not easy. It is for this very reason that you'll see numerous print queue shares, each with the same driver, pointing to the same port on print servers.
If you have say 10 printers shared across 100 devices, this isn't as much of an issue. You use login scripts, or Group Policy to deploy the print queues across all of your systems. But if you have 80 printers shared across 500 devices, it can more troublesome. It's not practical to add all the print queues to eachmachine, and you probably won't be able to use Group Policy to deploy the printers based on relative physical location of the client machine to the physical printer. Scripting to the Resuce!
Step 1: Create a mapping table in CSV formatCreate it in a format of <PathToOldPrintQueue>,<PathToNewPrintQueue>
\\printserver1\acnt_prntr_1,\\printserver1\AccountingPrinterPCL5 \\printserver1\acnt_prntr_2,\\printserver1\AccountingPrinterPCL5 \\printserver1\acnt_prntr_3,\\printserver1\AccountingPrinterPCL5
In this case, there may be three active queues on the print server, but you don't know which print queue is in use on which client device. We do know that, despite what the print queue installed is called, we really want it to be \\printserver1\AccountingPRinterPCL5
Step 2:The VBScriptApply the following VBScript via Group Policy, or just point to it from a logon script. Basically, it parses the CSV file provided in Step 1 into an array (don't forget to specify the path to said CSV file in the script!!). It then creates a list of all the print queues installed on the local machine. It loops through that list, and if the print queue exists in the mapping table, it will remove it then map it's replacement. All of this is logged to a file on the C:\
On Error Resume Next
'Save as a file with the .vbs extension such as printerConversion.vbs
'Script written by Daniel Westendorf - daniel at prowestech dot com. It is provided to be used and minipulated at
'will, but with any expressed warranty as to what it will or will not do. No support is provided with this script.
'Usage:
'Save as a file with the .vbs extension such as printerConversion.vbs
'Replace PATHTTOPRINTERMAPPINGCSVFILE with the real path to your mapping file. It should be in a location accessible
'by any machine such as \\domanNAME\NETLOGON
Dim objNetwork
Dim strComputer
Dim objWMIService
Dim objPrinter
Dim colInstalledPrinters
Dim defaultPrinter
Dim fs
Dim csvFile
Dim logFile
Dim printerArray
Dim printerChange
Dim i
printerArray = Array()
set fs = CreateObject("Scripting.FileSystemObject")
set csvFile = fs.OpenTextFile("PATHTTOPRINTERMAPPINGCSVFILE") 'Path to CSV file with format of \\OLDPRINTSERVER\PRINTER, \\NEWPRINTSERVER\REPLACEMENTPRINTQUEUE
i = 0
'Read CSV file and create MultiDimensional Array
Do Until csvFile.AtEndOfStream
ReDim Preserve printerArray(i)
printerChange = split(csvFile.Readline, ",") 'array of old, new printers, split by the comma
printerArray(i) = Array(printerChange(0), printerChange(1)) 'add printerChange to printerArray array at the next open slot
i = i + 1
Loop
csvFile.Close
'Create or open logfile, write date
If fs.FileExists("C:\PrinterSwap.log") Then
Set logFile = fs.OpenTextFile("C:\PrinterSwap.log", 8)
Else
Set logFile = fs.CreateTextFile("C:\PrinterSwap.log", 2)
End If
LogFile.WriteLine ("************************")
logFile.WriteLine ("------------------------")
logFile.WriteLine (Now)
logFile.WriteLine ("------------------------")
strComputer = "."
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")
For Each objPrinter in colInstalledPrinters 'loop through printers installed on system
For Each printerChange in printerArray 'for each printer, loop through the printerArray searching for a match
if LCase(objPrinter.Name) = LCase(printerChange(0)) Then 'check for match
objPrinter.Delete_
If Err.Number <> 0 Then
logFile.WriteLine ("Removed: " & objPrinter.Name)
Else
logFile.WriteLine ("Error removing printer: " & objPrinter.Name)
End If
Err.Clear
objNetwork.AddWindowsPrinterConnection(printerChange(1)) 'create new printer connection
If Err.Number <> 0 Then
logFile.WriteLine ("Added: " & printerChange(1))
Else
logFIle.WriteLine ("Error adding printer: " & printerChange(1))
End If
Err.Clear
if objPrinter.Default = "True" Then 'was that printer the default printer? Will only return true once.
defaultPrinter = printerChange(1)
End If
Err.Clear
End If
Next
Next
If defaultPrinter <> Empty Then
objNetwork.SetDefaultPrinter(defaultPrinter) 'set the default printer
If Err.Number <> 0 Then
logFile.WriteLine ("Set Default printer: " & defaultPrinter)
Else
logFile.WriteLine ("!!!Couldn't set default printer!!!")
End If
End If
LogFile.WriteLine ("************************")
logFile.Close
Step 3: Sit back and relax!
If the script runs, and it has access to the mapping table, it should replace the old print queues with your new queues!

No comments yet.