Solved

Need Help Scripting a User Mapped Printer Migration in Windows

Posted on 2012-03-20
1
548 Views
Last Modified: 2012-03-28
I successfully migrated all printer queues, printers, drivers, ports, etc from one Windows 2008 SP2 server to another using the "Print Management" MMC. I have tested the newly migrated printers and they work fine.

My objective is not to mograte the printers from one server to another - but to migrate all my users' mapped network printers to the new print server.

I found the following script by Jeff Wouters:
http://jeffwouters.nl/index.php/2011/09/a-script-to-migrate-user-mapped-network-printers/

I modified it to include my print servers and printers - as follows:

'Here comes a list with all printers identified by their old and new name.
'The first item is the old name and the second item is the new name.
Dim OldPrinterName(200)
Dim NewPrinterName(200)
dim objPrinter
OldPrinterName(0) = "\\printsrv01\HP Color LaserJet CM1312nfi MFP"
NewPrinterName(0) = "\\printsrv02\HP Color LaserJet CM1312nfi MFP"
OldPrinterName(1) = "\\printsrv01\HP Deskjet 6940 (Nicole's)"
NewPrinterName(1) = "\\printsrv02\HP Deskjet 6940 (Nicole's)"
OldPrinterName(2) = "\\printsrv01\HP Deskjet 6940 Diane"
NewPrinterName(2) = "\\printsrv02\HP Deskjet 6940 Diane"
OldPrinterName(3) = "\\printsrv01\HP LaserJet M1522nf"
NewPrinterName(3) = "\\printsrv02\HP LaserJet M1522nf"
OldPrinterName(4) = "\\printsrv01\HP LaserJet P2055dn PCL"
NewPrinterName(4) = "\\printsrv02\HP LaserJet P2055dn PCL"
OldPrinterName(5) = "\\printsrv01\HP LaserJet P3005 PCL6"
NewPrinterName(5) = "\\printsrv02\HP LaserJet P3005 PCL6"
OldPrinterName(6) = "\\printsrv01\KONICA MINOLTA C652SeriesFAX"
NewPrinterName(6) = "\\printsrv02\KONICA MINOLTA C652SeriesFAX"
OldPrinterName(7) = "\\printsrv01\KONICA MINOLTA C652SeriesPCL"
NewPrinterName(7) = "\\printsrv02\KONICA MINOLTA C652SeriesPCL"
OldPrinterName(8) = "\\printsrv01\KONICA MINOLTA C652SeriesPS"
NewPrinterName(8) = "\\printsrv02\KONICA MINOLTA C652SeriesPS"
OldPrinterName(9) = "\\printsrv01\KONICA MINOLTA C652SeriesXPS"
NewPrinterName(9) = "\\printsrv02\KONICA MINOLTA C652SeriesXPS"

'Running
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set Printers = WshNetwork.EnumPrinterConnections
dim printerIndex
printerIndex = 0

'Walk through the printers
For each printer in printers

'Determine the default printer
Dim objShell
Dim sPath
Dim sPrinter
Dim sPort
Dim sDriver
Dim ar_PrnInfo
Dim defaultPrinterName
Set objShell = CreateObject("WScript.Shell")
sPath = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
ar_PrnInfo = Split(objShell.RegRead(sPath), ",")
If IsArray(ar_PrnInfo) Then
sPrinter = ar_PrnInfo(0)
sPort = ar_PrnInfo(2)
sDriver = ar_PrnInfo(1)
End If
defaultPrinterName = UCase(sPrinter)


'Walk through the list of printers that need to be changed
For each ReplacePrinter in OldPrinterName
if UCase(printer) = UCase(ReplacePrinter) then
if Len(ReplacePrinter) then
WshNetwork.RemovePrinterConnection ReplacePrinter
WshNetwork.AddWindowsPrinterConnection NewPrinterName(printerIndex)
'WScript.echo "WshNetwork.RemovePrinterConnection"  & ReplacePrinter
'WScript.echo "WshNetwork.AddWindowsPrinterConnection"  & NewPrinterName(printerIndex)
end if
printerIndex = printerIndex + 1
end if
Next
'The following sleep is to avoid timeouts with drivers that take a lot of time to install
WScript.Sleep 1000
'objPrinter.SetDefaultPrinter(defaultPrinterName)
next

'Set the default printer
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set Printers2 = WshNetwork.EnumPrinterConnections
Dim printerNameWithoutServer
Dim tempArray
Dim printerNameWithoutServerNew
Dim tempArrayNew

'Walk through all the printers
For each printer in printers2
If InStr(defaultPrinterName,"\") <> 0  Then
tempArray = Split(defaultPrinterName, "\")
printerNameWithoutServer = tempArray(UBound(tempArray))
Else
printerNameWithoutServer = ""
End If
if InStr(printer,"\") <> 0 then
tempArrayNew = Split(printer,"\")
printerNameWithoutServerNew = tempArrayNew(UBound(tempArrayNew) )
else
printerNameWithoutServerNew = ""
end if
if (printerNameWithoutServerNew <> "") and (UCase(printerNameWithoutServer) = UCase(printerNameWithoutServerNew))Then
WshNetwork.SetDefaultPrinter(printer)
MsgBox "The network printers have been moved to a new server and printer " + (printerNameWithoutServer)+ " is still your default printer"
end if
Next

Open in new window


I had to comment-out the following line:
objPrinter.SetDefaultPrinter(defaultPrinterName)
...because it kept generating an "Object Required" error.
This is likely a cause for the script not completely working - though it at least runs without errors now.

The works thus far:
It correctly removes all printers that were mapped to the old print server.
It correctly counts the number of mapped network printers, and remembers that.
It maps the right _number_ of network printers to new print server.
...thats about it.

Where it fails:
It doesn't seem to remember the default printer.
It simply maps good network printers in order on the list - rather that the correct printers.
The 'printerIndex' is not incrementing, so it always adds printer(0).

I feel like I'm so close on this - just need another nudge in the right direction.
Thanks for your advice,
Neil
0
Comment
Question by:neilpage99
1 Comment
 
LVL 9

Accepted Solution

by:
neilpage99 earned 0 total points
Comment Utility
I wasn't asking for someone to write me a script - just point out where I went wrong with mine.

Nevermind, 8 days later no comments. I'll close it.
Seems to be the trend.
0

Featured Post

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now