Add printer with vbscript and enable snmp on tcp port

Table of Contents

I like using VB Script to automate tasks and use them on logon scripts.


strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

‘Install TCP/IP Port :

Set objNewPort = objWMIService.Get(“Win32_TCPIPPrinterPort”).SpawnInstance_

objNewPort.Name = “IP_192.168.1.10” ‘Change to TCP/IP port name you want created.
objNewPort.Protocol = 2
objNewPort.HostAddress = “192.168.1.10” ‘ Change to the ip address of the printer or the hostname
objNewPort.PortNumber = “515” ‘ Change to the port number normally 9100 or 515 for brother printers
objNewPort.Queue = “BINARY_P1” ‘ Change to Queue name
objNewPort.SNMPCommunity = “public” ‘ Normally just public
objNewPort.SNMPEnabled = True
objNewPort.Put_

‘Stop Spooler Service

Set colServiceList = objWMIService.ExecQuery _
(“Select * from Win32_Service where Name=’Spooler'”)
For Each objService In colServiceList
errReturn = objService.StopService()
WScript.Sleep 5000
Next

‘Start Spooler Service

For Each objService In colServiceList
errReturn = objService.StartService()
WScript.Sleep 5000
Next

‘Install Driver :

objWMIService.Security_.Privileges.AddAsString “SeLoadDriverPrivilege”, True

Set objDriver = objWMIService.Get(“Win32_PrinterDriver”)
objDriver.Name = “Brother MFC-6490CW Printer” ‘ Change to the driver name that matches INF file
objDriver.SupportedPlatform = “Windows NT x86”
objDriver.Version = “3”
objDriver.FilePath = “s:\printer\” ‘ Path to the print drivers
objDriver.InfName = “s:\printer\brprbh97.inf” ‘ Path to the INF file
intResult = objDriver.AddPrinterDriver(objDriver)

‘Install TCP-Printer :

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)

Set objPrinter = objWMIService.Get(“Win32_Printer”).SpawnInstance_

objPrinter.DriverName = “Brother MFC-6490CW Printer” ‘ Must match driver name
objPrinter.PortName = “IP_192.168.1.10” ‘ Must match port name created above
objPrinter.DeviceID = “Brother MFC-6490CW Printer” ‘What ever you want to name the printer
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.ShareName = “”
objPrinter.Published = False
objPrinter.Location = “A3 Printer Upstairs” ‘Location of the printer
objPrinter.Put_

Wscript.Quit

Our Services