Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I connect users to specific remote printers?

User Administration

How do I connect users to specific remote printers?

by  sparkbyte  Posted    (Edited  )
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.


Code:
'****************************************************
'*  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 [red][b]strPrnServer[/b][/red] 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#
        Else
        End If 

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

Dim strPrnServer: [red][b]strPrnServer[/b][/red] = "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))
Next

' 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 
Next

'*******************************************************

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.")
    WScript.Quit
End Sub

'*******************************************************
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top