Does anyone have a script. Bat to map network printer.
Does anyone have a script. Bat to map network printer to local authentication on windows?
Thanks in advance!
Answers (2)
Below is a tried & true vbscript that has server me well.
Similar scripts are floating around. My version doesn't apply any print spool settings; look around the net for that piece if needed.
My typical usage is to run/script execution from a network share.
See the script comments regarding where to place your printer drivers (you can either download or pull from your printers' installation media).
Script is provided as-is, and you must provide your own printer drivers, disclaimer this, not responsible for any damages that, etc. etc.
Option Explicit 'VBScript "MapPrinter_{your printer name/loc here}.vbs''Purpose:' Install and Map printer as spec'd in code, including installation' of printer drivers from a local directory. This version does not' contain all the code for enabling print spooling (look around the' internet for that if you need it...''Dependencies - Files/Objects' > Printer driver(s) for the printer to be installed, both x64/x86' as needed. The drivers must be signed.' Current version of this script expects the drivers to be in a' "Printer_Drivers" subfolder in the same directory as this script: ' \Printer_Drivers\Lanier LD140\x64' \Printer_Drivers\Lanier LD140\x86''Dependencies - User permissions' If executed in the context of local user, they may require' either Admin rights or software installation privileges;' have not tested this to date (I always use in context of domain admin)''Ver. 20?? Carl Hager, derived from multiple pre-existing scripts "around the Net" + my own additions. 'DECLARATION Dim obj_WMI_Svc Dim obj_WshShell Dim obj_WshNetwork Dim obj_FSO Dim obj_PrnPort Dim obj_Driver Dim obj_Printer Dim obj_colPrinters 'Dim obj_SpoolerSvc Dim int_Result Dim str_PrnName Dim str_PrnLocation Dim str_PrnComment Dim str_PrnDriverName Dim str_PrnDriverPath Dim str_InfName Dim str_PortIP Dim str_PortName Dim str_Computer Dim b_Is64Bit 'Dim b_UsePrnSpooler Dim b_Debug 'INIT b_Debug = False 'b_UsePrnSpooler = False str_Computer = "." Set obj_WMI_Svc = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & str_Computer & "\root\cimv2") Set obj_WshShell = CreateObject("WScript.Shell") Set obj_WshNetwork = CreateObject("WScript.Network") Set obj_FSO = createObject("scripting.fileSystemObject") 'Set whether OS is x64 'I know... this can be pulled via WMI but whatev's: b_Is64Bit = obj_FSO.FolderExists("C:\Program Files (x86)") str_PrnDriverPath = ".\Printer_Drivers" str_PrnDriverPath = str_PrnDriverPath & "\Lanier LD140" 'MUST set to appropriate Driver folder 'The "*.inf" path and actual driver can both vary depending on the OS architecture: If b_Is64Bit Then str_PrnDriverPath = str_PrnDriverPath & "\x64" 'If unsure if driver name, look in the printer installation software inf 'file (typically something like "oemsetup.inf" or similar) for a line 'reading something like: DrvName = "PCL6 Driver for Universal Print" str_PrnDriverName = "PCL6 Driver for Universal Print" Else str_PrnDriverPath = str_PrnDriverPath & "\x86" str_PrnDriverName = "LANIER MP 4001/LD140 PCL 5e" End If str_PrnName = "Lanier LD140" 'Set to desired Printer Name str_PrnLocation = "Rm 217" 'Set to desired Printer Location str_PrnComment = "Lanier LD140" 'Set to desired Printer Comment str_InfName = str_PrnDriverPath & "\oemsetup.inf" 'MUST point to the correct *.inf file for the printer being mapped str_PortIP = "{your ip address here}" 'MUST point to the IP Address of the printer on the network str_PortName = "IP_" & str_PortIP 'PROC On Error Resume Next 'Don't Install Printer if it's already installed: Set obj_colPrinters = obj_WMI_Svc.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & str_PortIP & "' ") For Each obj_Printer in obj_colPrinters MsgBox "Printer Port 'IP_" & str_PortIP & "' already installed", vbExclamation + vbSystemModal, " ALREADY INSTALLED" WSCript.Quit 114001 Next 'Handle Errors: If Err.Number <> 0 Then MsgBox "Error searching for existing Printers." & vbCrLf & _ "Err# " & Err.Number & " : " & Err.Description Err.Clear() WSCript.Quit End If 'Set "Load Driver" Privilege '(Shouldn't be required, but uncomment if needed): 'obj_WMI_Svc.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True 'Install Printer Port; Note that the drivers must be signed to install via WMI: Set obj_PrnPort = obj_WMI_Svc.Get("Win32_TCPIPPrinterPort").SpawnInstance_ obj_PrnPort.Name = "IP_" & str_PortIP obj_PrnPort.Protocol = 1 obj_PrnPort.HostAddress = str_PortIP obj_PrnPort.PortNumber = "9100" obj_PrnPort.SNMPEnabled = False obj_PrnPort.Put_ obj_WshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & _ str_PrnName & """ /f """ & _ str_InfName & """ /r ""IP_" & _ str_PortIP & """ /m """ & _ str_PrnDriverName & """", 1, True 'Handle Errors: If Err.Number <> 0 Then MsgBox "Error Setting Port or Drivers." & vbCrLf & _ "Err# " & Err.Number & " : " & Err.Description Err.Clear() WSCript.Quit End If 'Install Printer: Set obj_colPrinters = obj_WMI_Svc.ExecQuery("Select * From Win32_Printer Where DeviceID = " & Chr(34) & Replace(str_PrnName,"'","''") & Chr(34)) For Each obj_Printer In obj_colPrinters obj_Printer.Location = str_PrnLocation obj_Printer.Put_ Next 'Handle Errors: If Err.Number <> 0 Then MsgBox "Error Installing Printer." & vbCrLf & _ "Err# " & Err.Number & " : " & Err.Description Err.Clear() WSCript.Quit End If 'Make Default Printer if desired: int_Result = MsgBox(vbCrLf & "Make this the default printer?" & vbCrLf, vbYesNo, " PLEASE CHOOSE") If int_Result = vbYes Then obj_WshNetwork.SetDefaultPrinter str_PrnName Err.Clear() End If 'Clean-Up: Set obj_WMI_Svc = Nothing Set obj_FSO = Nothing Set obj_Driver = Nothing Set obj_PrnPort = Nothing Set obj_Printer = Nothing 'Set obj_SpoolerSvc = Nothing WScript.Quit 0
C. Hager
Comments:
-
Well, all my line breaks disappeared; Not sure if that's normal when using the code tags or not. If anyone wants, I'll post a version w/ {LineBreak} where all the CrLf's are and Notepad++ or similar can be used to replace them with actual linebreaks... - chager 10 years ago