Project Objective:
Reduce total cost of ownership/administration time on adding/replacing printers by automatically adding them at logon.

Problem to be solved:
This script replaces my existing batch script that uses con2prt.exe. The problem with con2prt.exe is that it isn’t great at removing network printers without removing PDF writers and or local attached printers.

Future Versions:
At some point I’d like to revisit this script and build some more intelligence into site and printer selection. I’d like for it to leverage AD to determine a system’s or user’s site then install printers accordingly.

'
' Printers-Site-Install.vbs
'
' Written by Aaron Wurthmann (aaron (AT) wurthmann (DOT) com)
'
' If you edit please keep my name as an original author and
' keep me apprised of the changes, see email address above.
' This code may not be used for commercial purposes.
' You the executor, runner, user accept all liability.
' This code comes with ABSOLUTELY NO WARRANTY.
' You may redistribute copies of the code under the terms of the GPL v2.
' -----------------------------------------------------------------------
' 2007-05-02 version 2
'
' Summary:
' Reduce total cost of ownership/administration time on adding/replacing 
' printers by automatically adding them at logon.
'
' -----------------------------------------------------------------------


Dim WshShell, oExec, output
Dim fso, tmpFile
Dim strComputerName
Dim strBuff
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
Set objADSysInfo = CreateObject("ADSystemInfo")

' EDIT THIS SECTION WITH YOUR INFO
' If Site X then Printer X, if Site Y then Printer Y
if objADSysInfo.SiteName="San-Jose-Site" then
 strSiteServer="MyPrintServer"
end if

' END EDIT SECTION
 
' Remove Any Other (other than Site Print Server) Network Printers
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer WHERE Name LIKE '%\\\\%'")
For Each objPrinter in colInstalledPrinters
 ' WScript.Echo objPrinter.Name
 tmp = InStr(1, objPrinter.Name, strSiteServer, 1)
 if tmp = 0 Then
  WshNetwork.removePrinterConnection objPrinter.Name, true, true
 end if
Next
' Enumerate Site Printer Server and Add Connections if Needed
Set oExec = WshShell.Exec("net view \\" & strSiteServer)
strBuff = oExec.StdOut.ReadAll
arrayBuff = split(strBuff, vbCrLf, -1, 1)
for each buff in arrayBuff
        result = InStr(1, buff, "Print ", 1)
        if result > 0 Then
         printerName = split(buff, " ", -1, 1)(0)
          connName = "\\" & strSiteServer & "\" & printerName
                 WshNetwork.AddWindowsPrinterConnection connName
        end if
next

 
Be Sociable, Share!