2008-09-04

VBScript Powered HTML Application to Install Printers

This is a handy little application every IT help desk needs. The number one reason an employee will call our department is to request a printer be installed. Maybe they deleted it, maybe they got a new printer, or maybe they traveled to another branch. Whatever the reason, it's time consuming and painfully monotonous.

That's why I developed this great HTA (HTML Applicaton) powered by a VBScript that makes use of WMI to install a printer. Basically, an HTA is a container for website, but it's a website that is unaffected by browser security features (like script blocking). Unfortunately this doesnot work "out of the box". You don't need this exact setup, but this is what you won't seewhen you look at my script:

I have two databases this HTA connects to. We have a large number of remote branches and we support them all, so one database is all branch names and numbers. The other is a database of printer types and IP addresses all tied to a corresponding branch ID.

I have the printer drivers stored on a network share for easy manual access and updating as needed.

Various graphics are used to make things pretty.

I have had to strip out the HTML so you can actually see the script, but that's OK, the script is the meat of this post.

Each printer type will require some testing. If an .INF file is supplied as the driver, you have it easy. However, if you look at the second printer in this script, you will see some special code is required for others. In this example, HP only supplies .EXE files, but there are some special commands you can use to run it silently.

To transform this into a functioning HTA with a GUI you simply need to add HTML tags and an HTA tag block to it. Then save it as Whatever.HTA. Just be sure to alter the script as necessary if you name your listboxes and button differently or decide you don't need multiple printer types or a location listbox.

If you need any clarification, feel free to comment. I'm happy to help!





'------------------------------
'Author: Christopher Maddalena
'Date: December 20, 2007
'Purpose: Create a TCP/IP port for a printer and install it using a non-natice driver.
'How: The script creates a TCP/IP port, retrieves the .INF driver file and performs the
' Add Printer task by using a specified .INF, name and IP address with WMI.
'------------------------------

'Create the connection to the database
Set objConn = CreateObject("ADODB.Connection")
Set objConn2 = CreateObject("ADODB.Connection")

'Open the connection to the database
objConn.Open "ConnectionString"
objConn2.Open "COnnectionString 2"

On Error Resume Next
Err.Clear

'Commands executed on load
Sub window_onLoad()
'Resize the window to 525x550
window.resizeTo 525,600

'Populate listbox (lstBranch) with branch names from database
Set objRecordset = CreateObject("ADODB.Recordset")
'Open a dynamic recordset (allows for updating, movePrevious, and more)
objRecordset.Open "SQL statement", objConn, 2
'Create new listbox options until the end of the database rows
Do Until objRecordset.EOF = True
Set objOption = Document.createElement("OPTION")
objOption.Text = objRecordset("Branch # 'n Such") & " - " & objRecordset("Branch Location or City")
objOption.Value = objRecordset("Branch #")
lstBranch.Add(objOption)
objRecordset.MoveNext
Loop
objRecordset.Close

objRecordset.Open "SQL statement", objConn2, 2
'Create new listbox options until the end of the database rows
Do Until objRecordset.EOF = True
Set objOption = Document.createElement("OPTION")
objOption.Text = objRecordset("The printer type or name")
objOption.Value = objRecordset("A unique ID")
lstPrinter.Add(objOption)
objRecordset.MoveNext
Loop
objRecordset.Close
End Sub

Sub btnSubmit_onClick()
Dim objFileSystem, objShell, objConn, objXerox6180Driver, objCommand, objCommandType, objRecordset, objWMIService, objNewPort, objPrinter, objDriver
Dim intResult, ip, portName, strComputer

Set objRecordset = CreateObject("ADODB.Recordset")
Set objShell = CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")

Select Case lstPrinter.Value
'Nothing selected
Case 0
MsgBox("You must select a printer.")
'Xerox Phaser 6180MFP
Case 1
Set objXerox6180Driver = objFileSystem.GetFolder("\\Path to Driver\Xerox 6180MFP\")

objRecordset.Open "SQL statement with listbox values", objConn2, 2

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConn2
objCommand.CommandText = "SQL statement with listbox values"
objCommandType = 1

objRecordset.Close

Set objRecordset = objCommand.Execute

'Check if recordset is empty
'If it is empty, display error dialog or install printer otherwise
If objRecordSet.EOF AND objRecordset.BOF Then
MsgBox("The branch you selected does not have an entry for a Xerox 6180MFP. Please try again.")
Else
objFileSystem.CopyFolder objXerox6180Driver.Path, "C:\", True

ip = objRecordset.Fields("The IP address").Value
portName = "IP_" & ip

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set objNewPort = objWMIService.get("Win32_TCPIPPrinterPort").SpawnInstance_
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Set objDriver = objWMIService.Get("Win32_PrinterDriver")

'Installs Printer Driver
objDriver.Name = "Xerox Phaser 6180MFP-N PS"
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
objDriver.FilePath = "C:\Xerox 6180MFP"
objDriver.InfName = "C:\Xerox 6180MFP\xrpsgbei.inf"
intResult = objDriver.AddPrinterDriver(objDriver)

'Installs Printer Port
objNewPort.Name = portName
objNewPort.Protocol = 1
objNewPort.HostAddress = ip
objNewPort.PortNumber = 9100
objNewPort.SNMPEnabled = True
objNewPort.Put_

'Install Printer
objPrinter.DriverName = "Xerox Phaser 6180MFP-N PS"
objPrinter.PortName = portName
objPrinter.DeviceID = lstBranch.Value & " - " & "Xerox 6180 MFP"
objPrinter.Location = ""
objPrinter.Network = True
objPrinter.Put_

MsgBox("Your printer should now be installed. If the following number is NOT zero, please restart and try again if your printer is missing. If it continues, contact the help desk. #: " & intResult)
End If
'HP 7780 All-In-One
Case 2
'Create and open Postgres connection
Set objConn = CreateObject("ADODB.Connection")

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConn
objCommand.CommandText = "SQL statement with listbox values"
objCommandType = 1

Set objShell = CreateObject("WScript.Shell")

Set objRecordset = objCommand.Execute

'Check if recordset is empty
'If it is empty, display error dialog or install printer otherwise
If objRecordset.EOF AND objRecordset.BOF Then
MsgBox("The branch you selected does not have an entry for an HP 7780. Please try again.")
Else

ip = objRecordset.Fields("IPAddress").Value
portName = "IP_" & ip

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set objNewPort = objWMIService.get("Win32_TCPIPPrinterPort").SpawnInstance_
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
Set objDriver = objWMIService.Get("Win32_PrinterDriver")

'Due to the nature of the setup utility and drivers this command must be run to automatically install the 7780
objShell.Run "\\Path to driver\HP7780\\setup.exe -s -pi -p ""C:\HP"" -ipaddress " & ip & " -addadevice -r0"
End If
End Select
End Sub

'Exit button sub
Sub btnExit_onClick()
'Close the connection to the database
objConn.Close

Window.Close
End Sub

4 comments:

Jake Phillips said...

This is very helpful to the IT people. Installing a printer over and over again is painfully monotonous. This will save time.

offset printing

Unknown said...

Nice one! like it! Thanks! cross platform mobile development

sandeep saxena said...


Thank you for your so cool post,it is useful,i love it very much.please share with us more good articles.
QTP Training in Chennai
QTP Course in Chennai
QTP Training in Tambaram
QTP Training in OMR
LoadRunner Training in Chennai
Best Loadrunner training institute in chennai
javascript training in chennai
core java training in chennai

Mithun said...

Heartily Thanks for all your Works...To Explore more about Technically Visit here and Enhance more about it...
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery