Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBScript FAQ

User Administration

How do I connect users to specific remote printers? by sparkbyte
Posted: 6 Nov 07

This is a script that I hope would of interest to this group.  I have seen MANY requests for scripts to connect to individual remote printers based on "some" criteria.  I developed this script to connect to remote printers on our print server based on a users branch number.

Thanks are due to dm4ever,PHV,tsuji and many others for their contributions over the past year or so with my many questions.

Hopefully someone will find this usefull.

'*  Script by:  John Fuhrman
'*    Date:        2007, Nov 6
'*    Description:
'*    This script connects to printers based on a users
'*    branch number using a dictionary object. The
'*    script also will not reconnect to an already
'*    connected printer.
'* The strPrnServer value will need to be set to
'* whatever your printer servers name or IP is.
Option Explicit

Dim WSHShell: Set WSHShell = CreateObject("WScript.Shell")
Dim WSHNetwork: Set WSHNetwork = CreateObject("WScript.Network")
Dim fso: Set fso = CreateObject("scripting.filesystemobject")

'Grab the user name
Dim UserString: UserString = WSHNetwork.UserName

'strUser = objUser.Name
Dim strBranch: strBranch = (Left(UserString,3)) ' Grab the first 3 characters of the username

'    If the user does not have a branch number prefix prompt for
'    the user the the branch number.
        If IsNumeric(strBranch) = False Then
            Call UserPrompt() ' function sets the user bank#
        End If

Dim objDictPrnts: Set objDictPrnts = CreateObject("Scripting.Dictionary")
objDictPrnts.CompareMode = vbTextCompare

Dim strPrnServer: strPrnServer = "lxolcdprn01"
Dim objWMIService: Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strPrnServer & "\root\cimv2")

Dim colInstalledPrinters: Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

Dim objPrinter
For Each objPrinter in colInstalledPrinters
' create a dictionary to store our printer paths
Dim strBKtemp: strBKtemp = UCase(Mid(objPrinter.Name,1,3))
objDictPrnts.Add "\\" & strPrnServer & "\" & objPrinter.Name, UCase(Mid(objPrinter.Name,1,3))

' loop through the path's that were not found and add them
Dim PrinterPath
For Each PrinterPath In objDictPrnts.Keys
Dim strBkPrinter: strBkPrinter = UCase(PrinterPath)
    If objDictPrnts.Item(strBkPrinter) = strBranch Then
        WSHNetwork.AddWindowsPrinterConnection PrinterPath
    End If


Function UserPrompt()
UserPrompt = InputBox("User's numeric prefix not identified." &_
    vbcrlf & vbcrlf & "Enter the User's Branch Number.")
    If UserPrompt = False Then Call UserError(1)
    If UserPrompt = "" Then Call UserError(2)
    strBranch = UserPrompt
End Function


Sub UserError(Error)
    If Error = "1" Then MsgBox("Canceled")
    If Error = "2" Then MsgBox("User's Branch Number not entered.")
End Sub


Back to VBScript FAQ Index
Back to VBScript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close