Para la instalación automática de impresoras en equipos Windows utilizaremos un script en Visual Basic Script que a continuación veremos.
Lo primero será hacernos de los controladores del fabricante para nuestro modelo de impresora. En este ejemplo se trata de una impresora Kyocera FS 4200 DN. Una vez tengamos el controlador del fabricante tendremos que localizar el fichero INF que contiene la información para la instalación de la impresora. Por lo que tendremos que localizar dentro del fichero el nombre exacto con que el fabricante identifica el modelo de impresora en cuestión, en nuestro caso es: "Kyocera FS-4200DN KX"
Como pretendemos que el script se pueda utilizar en las diferentes versiones de Windows, vamos a utilizar una función llamada FindOSType que nos ayude con dicho objetivo.
Al comienzo del script para las versiones superiores a windows XP necesitamos que este se auto llame solicitando privilegios de administrador para poder realizar la instalación de los controladores de la impresora.
Luego hacemos uso del procedimiento MapLPRPrinter para que se genere el puerto de la impresora y se instale la impresora. La llamada al procedimiento tendremos que adaptarla para nuestra impresora o impresoras (pues podríamos instalar todas las impresoras que nos hagan falta). Los parámetros serán:
- el nombre exacto que nos indicó el fabricante de la impresora,
- la trayectoria a los controladores (en el ejemplo hemos asumido que están en el mismo lugar que el script)
- el nombre exacto del fichero INF.
- IP que deseamos asignar a la impresora
- nombre que deseamos asignar a la impresora
- nombre de la cola
Script completo para la instalación de una impresora Kyocera FS 4200 DN:
Set WshShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
strComputer = WshShell.ExpandEnvironmentStrings("%computername%")
OSType = FindOSType(strComputer)
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 And OSType <> "Windows XP" Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
WScript.Quit
End If
MapLPRPrinter "Kyocera FS-4200DN KX", oFSO.GetParentFolderName(wscript.ScriptFullName) & "\" , "\OEMSETUP.INF", "192.168.1.175","Impresora kyocera","followme"
MapLPRPrinter "Kyocera ECOSYS P6026cdn KX",
Wscript.Echo "Finalizada la instalación."
Sub MapLPRPrinter(strDriverName,strDriverInfPath,strDriverInf,strIPAdr,strPrinterName,strQueue)
Const PathArc = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
on Error Resume Next
'SETS PRINTER PORT.
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = strPrinterName & strIPAdr
objNewPort.PortNumber = "515"
objNewPort.Protocol = 2
objNewPort.HostAddress = strIPAdr
objNewPort.Queue = strQueue
objNewPort.ByteCount = True
objNewPort.SNMPEnabled = False
objNewPort.Put_
NewPort = objNewPort.Put_
'INSTALLS DRIVER NOT LOCATED IN Driver.cab
Set objDriver = objWMIService.Get("Win32_PrinterDriver")
objDriver.Name = strDriverName
if GetObject(PathArc).AddressWidth = 32 then
'WScript.Echo "Arquitectura 32bits"
objDriver.DriverPath = strDriverInfPath & "32"
objDriver.FilePath = strDriverInfPath & "32"
objDriver.Infname = strDriverInfPath & "32" & strDriverInf
objDriver.SupportedPlatform = "Windows NT x86"
else
'Wscript.Echo "Arquitectura 64bits"
'objDriver.DriverPath = strDriverInfPath & "64"
objDriver.FilePath = strDriverInfPath & "64"
objDriver.Infname = strDriverInfPath & "64" & strDriverInf
objDriver.SupportedPlatform = "Windows x64"
end if
objDriver.Version = "3"
intResult = objDriver.AddPrinterDriver(objDriver)
If intResult >0 Then
WScript.Echo "Error en la instalación codigo: " & intResult & _
" Carpeta del driver: " & objDriver.DriverPath & _
" INFO: " & objDriver.Infname & _
" Driver: " & strDriverName
End If
'SETS PRINTER TO PORT.
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
objPrinter.Name = strPrinterName
objPrinter.DriverName = strDriverName
objPrinter.PortName = strPrinterName & strIPAdr
objPrinter.DeviceID = strPrinterName
objPrinter.CurrentLanguage = 2
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.EnableBIDI = False
objPrinter.Put_
End Sub
Function FindOSType(strComputer)
'Defining Variables
Dim objWMI, objItem, colItems
Dim OSVersion, OSName, ProductType
'Get the WMI object and query results
Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
'Get the OS version number (first two) and OS product type (server or desktop)
For Each objItem in colItems
OSVersion = Left(objItem.Version,3)
ProductType = objItem.ProductType
Next
'Time to convert numbers into names
Select Case OSVersion
Case "6.1"
OSName = "Windows 7"
Case "6.0"
OSName = "Windows Vista"
Case "5.2"
OSName = "Windows 2003"
Case "5.1"
OSName = "Windows XP"
Case "5.0"
OSName = "Windows 2000"
Case "4.0"
OSName = "Windows NT 4.0"
Case Else
OSName = "Windows 9x"
End Select
'Return the OS name
FindOSType = OSName
'Clear the memory
Set colItems = Nothing
Set objWMI = Nothing
End Function
Fuente de información:
http://gallery.technet.microsoft.com/scriptcenter/Add-a-LPR-Printer-include-f462f332
http://www.leinss.com/blog/?p=1098
Comentarios