« Archives in August, 2011

Add an Active Directory printer VBScript

The area that I support one day needed multiple Active Directory (AD) printers mapped for multiple users on multiple machines.  I wanted a nice uniform way to add all these printers quickly without bothering the end user.  The VBScript that I wrote was placed in the “All Users” startup folder and will be executed by each user automatically when they log on to the system. By placing all the printer mappings in a file I can easily manage adding, deleting, and setting default printers remotely for a user or for all users on that PC. Please see the script and the corresponding data file below.


Option Explicit
CONST ForReading = 1

Dim ComputerName, UserName, objShell, objNetwork, PrinterMapFile, objFSO, strData
Dim pComputer, pUser, pPrinter, pAction, CharacterCount, arrLines, strLine
Dim posn1, posn2, posn3

Set objNetwork = CreateObject("WScript.Network")
Set objShell = WScript.CreateObject("WScript.Shell")

Sub forceUseCScript()
  If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
    objShell.Run "cmd.exe /c " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & _
    WScript.scriptFullName & Chr(34),1,False
    WScript.Quit 0
  End If
End Sub

forceUseCScript

' Get the computer's name.
ComputerName = UCase(objNetwork.ComputerName)
UserName = UCASE(objShell.ExpandEnvironmentStrings("%USERNAME%"))

Wscript.Echo "Printer Mapper v100a"
Wscript.Echo "Company Name Here"
Wscript.Echo " "

'name of the text file
PrinterMapFile = "C:\printer.map"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check to see if File Exists, if not then exit right there.
If NOT objFSO.FileExists(PrinterMapFile) then
  wscript.quit
end if

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(PrinterMapFile,ForReading).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Step through the lines
For Each strLine in arrLines
  CharacterCount = Len(strLine) - Len(Replace(strLine, ",", ""))
  if Left(strLine, 1) <> "'" and CharacterCount = 3 then
    posn1 = InStr(1, strLine, ",", 1)
    posn2 = InStr(posn1+1, strLine, ",", 1)
    posn3 = InStr(posn2+1, strLine, ",", 1)
    pComputer = UCase(Left(strLine, posn1-1))
    pUser = UCase(Mid(strLine, posn1+1, posn2-posn1-1))
    pPrinter = Mid(strLine, posn2+1, posn3-posn2-1)
    pAction = Right(strLine, Len(strLine) - posn3)

    if pComputer = ComputerName then
      if pUser = UserName or pUser = "ALLUSERS" then
        ' Add and make it default
        if pAction = "D" then
          Wscript.Echo "Adding/Default: " & pPrinter
          objNetwork.AddWindowsPrinterConnection pPrinter
          objNetwork.SetDefaultPrinter pPrinter
        end if

        ' Add
        if pAction = "A" then
          Wscript.Echo "Adding: " & pPrinter
          objNetwork.AddWindowsPrinterConnection pPrinter
        end if

        ' Remove
        if pAction = "R" then
          Wscript.Echo "Removing: " & pPrinter
          objNetwork.RemovePrinterConnection pPrinter
        end if
      end if
    end if
  end if
Next

'Cleanup
Set objFSO = Nothing

<—-Data File—->

' Computer Name,User,PRINTSVR Printer,A(dd), D(efault)/Add, R(emove)
E-CARD-STAFF1,ALLUSERS,\\PRINTSVR\E-EL-COLOR1-PS,A
E-CARD-STAFF2,ALLUSERS,\\PRINTSVR\E-EL-COLOR1-PS,A
E-CARD-STAFF3,ALLUSERS,\\PRINTSVR\E-EL-COLOR1-PS,A
E-CARD-STAFF4,ALLUSERS,\\PRINTSVR\E-EL-COLOR1-PS,A
E-CARD-STAFFMGR,ALLUSERS,\\PRINTSVR\E-EL-COLOR1-PS,D
E-CARD-STAFF5,MRH001,\\PRINTSVR\E-CARD-MFD-PS,A

<—-Data File—->