Please note that I didn’t use this script since the PrintNightmare saga started.
A long time ago, I found this VBS script on Microsoft Script Gallery. Unfortunately, this script is now nowhere to be found, thus, I decided to share it with you.
This script, once executed on a workstation, allow migrating all connected printers (\\printserver\sharedprinter) to a new server. While this script is probably not as fancy as some Powershell scripts, this one has never let me down and works every single time.
This script is easy to use, just update line 2 and execute it on the workstation with the remote management tool of your choice or through GPO.
' put in the server names here (keep the quotes!)
MovePrinters "Old Server","New Server"' ********************************************* ' Move printers to new server ' ********************************************* Sub MovePrinters(OldServer, NewServer)' Loops through all network printers and moves all printers ' on "OldServer" to the same printername on "NewServer". dim WshNetwork, oPrinters, i, PrinterPath, DefaultPrinter, PrinterList
Set WshNetwork = WScript.CreateObject("WScript.Network")Set PrinterList = CreateObject("Scripting.Dictionary")' Get the default printer before we start deleting:
DefaultPrinter = GetDefaultPrinter
' Get a list of printers to work with: ' (We cannot modify the collection while looping through it) Set oPrinters = WshNetwork.EnumPrinterConnections
For i =1to oPrinters.Count Step2
PrinterList.Add oPrinters.Item(i),"x"Next' i ' Loop through the printer list and migrate mathching ones: ForEach PrinterPath In PrinterList.Keys
If StrComp(ServerName(PrinterPath), OldServer,1)=0Then
WshNetwork.RemovePrinterConnection PrinterPath,True,TrueOnErrorResumenext
WshNetwork.AddWindowsPrinterConnection "\\"& NewServer &"\"&_
ShareName(PrinterPath)If Err.Number =0ThenSet objFile = wscript.CreateObject("Scripting.FileSystemObject")IfNot objFile.FolderExists("c:\printers_remapped")Then
objFile.CreateFolder "c:\printers_remapped"
objFile.Close
EndIfEndIf'If Err.Number = -2147023095 Then ' MsgBox "The printer """ & ShareName(PrinterPath) & _ ' """ does not exist on server """ & NewServer & """." & vbCrLf & _ ' "The printer has been removed.", vbOKonly + vbExclamation, _ ' "Missing printer" 'End If OnErrorgoto0EndIfNext'Set the default printer: If ServerName(DefaultPrinter)= OldServer ThenOnErrorResumeNext
WshNetwork.SetDefaultPrinter "\\"& NewServer &"\"&_
ShareName(DefaultPrinter)'If Err.Number = -2147352567 Then 'MsgBox "Your default printer did not exist, and has been deleted.", _ ' vbOKonly + vbInformation, "Invalid default printer" 'End If OnErrorgoto0EndIfEndSub' MovePrinters Function GetDefaultPrinter()' Returns the UNC path to the current default printer Dim oShell, sRegVal, sDefault
Set oShell = CreateObject("WScript.Shell")
sRegVal =_"HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
sDefault =""OnErrorResumeNext
sDefault = oShell.RegRead(sRegVal)
sDefault = Left(sDefault ,InStr(sDefault,",")-1)OnErrorGoto0
GetDefaultPrinter = sDefault
EndFunctionFunction ServerName(sPrinterPath)Dim aPrinterPath
ServerName =""If Left(sPrinterPath,2)="\\"Then
aPrinterPath = Split(sPrinterPath,"\")
ServerName = aPrinterPath(2)EndIfEndFunctionFunction ShareName(sPrinterPath)Dim aPrinterPath
ShareName =""If Left(sPrinterPath,2)="\\"Then
aPrinterPath = Split(sPrinterPath,"\")
ShareName = aPrinterPath(3)EndIfEndFunction'discardScript() Function discardScript()Set objFSO = CreateObject("Scripting.FileSystemObject")
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript)EndFunction'--------------------8<----------------------
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok