Icono del sitio RDR-IT

Migración del servidor de impresión

Migración del servidor de impresión

Para poder seguir la ejecución del script, los seguimientos se escriben en el visor de eventos de Windows.

Requisito previo: las impresoras deben tener el mismo nombre en ambos servidores.

On Error Resume Next

Dim strComputer, oldDefaultPrinter, oldDefaultPrinterSansServer, PrinterServerOld, PrinterServerNew, ProcessDefaultPrinter

strComputer = "."

' Define value old PrinterServer
PrinterServerOld = "SERVER_A"
PrinterServerNew = "SERVER_B"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oShell = CreateObject("WScript.Shell")

oShell.LogEvent SUCCESS, "===== START SCRIPT SWITCH PRINTER SERVER ====="

oldDefaultPrinter = DefaultPrinter	' On recupere l imprimante par default
oShell.LogEvent SUCCESS, "Default printer detected : " & oldDefaultPrinter

oldDefaultPrinterSansServer =  Right(oldDefaultPrinter, len(oldDefaultPrinter) - (len(PrinterServerOld)+3))

' Verification imprimante mappe sur le nouveau serveur
if IsPrinterConnected("\\"& PrinterServerNew &"\" & oldDefaultPrinterSansServer) = 0 then
	oShell.LogEvent SUCCESS, "Default printer not installed on : " & PrinterServerNew
	'Wscript.quit(0)
end if

' Check if need process switch default printer
' Only if default printer is in old server
if InStr(1,oldDefaultPrinter,PrinterServerOld,vbTextCompare) = 0 then
	ProcessDefaultPrinter = "N"
	oShell.LogEvent SUCCESS, "Ignore switch default printer not in old server"
else
	ProcessDefaultPrinter = "Y"
	oShell.LogEvent SUCCESS, "Process switch default printer"
end if

For Each objPrinter in colInstalledPrinters
	oShell.LogEvent SUCCESS, "Printer iteration : "& objPrinter.Name
	if lcase(Left(objPrinter.Name,2)) = "\\" then
		' Check printer in Each is on old printer server
		if( Left(objPrinter.Name, (len(PrinterServerOld)+2)) = "\\"& PrinterServerOld  OR Left(objPrinter.Name, (len(PrinterServerOld)+2)) = "\\"& Ucase(PrinterServerOld) ) then
            ' Verification imprimante de l ancien serveur est connectee sur le nouveau
            if IsPrinterConnected("\\"& PrinterServerNew &"\" & Right(objPrinter.Name, len(objPrinter.Name) - (len(PrinterServerNew)+3))) = 1 then
			    WshNetwork.RemovePrinterConnection objPrinter.Name, true, true
			else
				' Printer mapping on new server
				WshNetwork.AddWindowsPrinterConnection "\\"&PrinterServerNew &"\"& Right(objPrinter.Name, len(objPrinter.Name) - (len(PrinterServerNew)+3))
				oShell.LogEvent SUCCESS, "Mapping printer : "&  Right(objPrinter.Name, len(objPrinter.Name) - (len(PrinterServerNew)+3)) &" on new server"
				WshNetwork.RemovePrinterConnection objPrinter.Name, true, true
				oShell.LogEvent SUCCESS, "Remove printer on old server"
				WScript.Sleep(60000)
            end if
		end if		
	end if
Next

if ProcessDefaultPrinter = "Y" then
	oShell.LogEvent SUCCESS, "Start pause/sleep script for 3 min"
	WScript.Sleep(180000)
	oShell.LogEvent SUCCESS, "End pause/sleep"
	if IsPrinterConnected("\\"& PrinterServerNew &"\" & Right(oldDefaultPrinter, len(oldDefaultPrinter) - (len(PrinterServerNew)+3))) then
		WshNetwork.SetDefaultPrinter "\\"& PrinterServerNew &"\" & Right(oldDefaultPrinter, len(oldDefaultPrinter) - (len(PrinterServerNew)+3))
		oShell.LogEvent SUCCESS, "Switch default printer on new server"
	end if
end if

oShell.LogEvent SUCCESS, "===== END SCRIPT SWITCH PRINTER SERVER ====="

'##############################################################
'#						Functions							  # 
'##############################################################

Function DefaultPrinter
    Dim strComputer
    Dim Result
    
    strComputer = "."
    Result = ""
    
    Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colInstalledPrinters = objWMIService.ExecQuery _
     ("Select * from Win32_Printer")
    For Each objPrinter in colInstalledPrinters
        If objPrinter.Default = True Then
         Result = objPrinter.Name
        End If
    Next
    DefaultPrinter = Result
End Function

Function IsPrinterConnected (strPrinter)

	' declaration des variables
	Dim oWMI, oPrinter
	Dim colPrinters
	Dim strServer, strShare

	' initialisation des objets
	Set oWMI = GetObject ("winmgmts:\root\cimv2")

	' decomposition du nom de l'imprimante reseau
	' en nom de serveur et nom de partage
	strServer = "\\" & Left (strPrinter, InStrRev (strPrinter, "\") - 1)
	strShare = Mid (strPrinter, InStrRev (strPrinter, "\") + 1)

	' requete WMI pour determiner si l'imprimante specifiee
	' est connectee dans la session courante
	Set colPrinters = oWMI.ExecQuery ( _
	"SELECT * from Win32_Printer " & _
	"WHERE ServerName='" & strServer & "' " & _
	"AND ShareName='" & strShare & "'")

	' retour du resultat de la requete
	IsPrinterConnected = colPrinters.Count

End Function

Modifique las dos líneas siguientes según su entorno:

PrinterServerOld = "SERVER_A"
PrinterServerNew = "SERVER_B"
Salir de la versión móvil