Add Printers Using VBScript on an Intranet Site

Recently I had been tasked to create an Intranet site that simplified the process of adding printers to workstations. In addition, the process had to be completely in the user’s control, eliminating the need for IT to be involved with workstation printer drivers. If a user moved or a printer went down, the user would be responsible for adding a new network printer to his or her workstation. Below is a variation of the script I used…

VBScript

Adding VBScript to a web page is as easy as adding JavaScript; you will use the <script> tag with a different language type defined. The only additional requirement for getting VBScript to run properly is to ensure that your Intranet site is set as a Trusted Site in Internet Explorer — for obvious reasons this is required.

<SCRIPT>
<!--
      Function addPrinter(mynum)
          Dim mypath, myname

          On Error Resume Next

          Select Case (mynum)
               Case "1"
                   mypath = \\PRINTSERVER\PRINTER1
                   myname = "PRINTER1"
               Case "2"
                   mypath = \\PRINTSERVER\PRINTER2
                   myname = "PRINTER2"
          End Select

          Dim WSHNet
          Set WSHNet = CreateObject("WScript.Network")
          WSHNet.AddWindowsPrinterConnection mypath

          strMbox = MsgBox("Would you like to make " & myname & " your default printer?",3,"Printer")
          If strMbox = 6 Then
              WSHNet.SetDefaultPrinter mypath
          Else
              'nothing to do...
          End If

          If Err <> 0 Then
                MsgBox "Please contact the IT HelpDesk."
                Err.Clear
                Exit Function
          End If

          MsgBox "The printer, " & myname & ", has been installed on your workstation!"

      End Function

//-->
</SCRIPT>

Broken Down

For the people that are more than interested of just plagiarizing my code, I’ll explain the function in a little bit of detail. First, the function is called as you would call a JavaScript function, such as by using an onclick=”” method. Second, pass a variable to the function to define which printer should be installed.

In my case, I had drawn out simple floor plans of our facilities and placed printer icons in <div> tags throughout the map. When the user clicked on the icon, the VBScript function is initiated and the printer is installed. For those that are unfamiliar with the onclick method, you can apply it to many HTML objects, such as <a href> tag or <img> or <div>. If you apply onclick to a div statement, you will probably want to assign the style “cursor:hand;” to change the cursor to a hand pointer, letting the user know this is a clickable area.

The code uses a Windows Scripting namespace called WScript.Network. A defined method in WScript.Network, AddWindowsPrinterConnection, allows the scripter to easily add a networked printer to the workstation. In my script, the only variable passed to this method is the UNC path of the network printer. Shortly following the printer add method, the user is asked if he or she would like to make it the default printer. If true, then a defined method is WScript.Network, SetDefaultPrinter, is called with one variable passed to it, the UNC path to the network printer. On any type of error a generic message is display to contact the IT HelpDesk.

About the Author

IT is not just a job but also a passion. Everything I have accomplished, both personally and professionally, has been generally entertaining, bordering on fun. Some of my projects, such as working with SharePoint Services workflow actions in Visual Studio or building a custom iSCSI SAN using the OpenSolaris, ZFS and COMSTAR, has been quite rewarding. You may think nerd...I think developing a new trend!