Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Script to automate adding and removal of printers (and set additional preferences)

Posted on 2008-10-28
2
416 Views
Last Modified: 2013-11-10
Hi, I'm looking for a script (batch / wsh / ?) that will automate the adding and removal of network printers. In addition, it would be great to have the script set particular printing preferences, such as to set the Job Type to Hold and to set the Hold Print Details User ID for the user logged in.

Your help will be appreciated!
0
Comment
Question by:cyrax
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
Angelized earned 250 total points
ID: 22840606
Hello, I have something for you that can be a start of an autamtic installation of printer based on AD Computer object.
What you need to do is to set your print server name : strPrintServer = "."  
The script will list all printer on the print server and compare the computer description in ad.  If the computer description contains the name of the printer it will be installed on the computer.
This really a 1st raw, lemme know if you need more informartion on this.
 

strPrintServer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & PrintServer & "\root\cimv2")
 
Set objComputer = CreateObject("Shell.LocalMachine")
sComputername = objComputer.MachineName
 
Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
    If instr(ReturnComputerDescription(sComputername),objPrinter)>0 Then
       InstallPrinter(strPrintServer,objPrinter.Name,False)
    End If       
Next
 
 
Function InstallPrinter (sPrintServer,sPrinter,Default)
   Set WshNetwork = CreateObject("WScript.Network")
   Dim FullPrinterString
   FullPrinterString = "\\" & sPrintServer & "\" &  sPrinter
   WshNetwork.AddWindowsPrinterConnection FullPrinterString
   If default=True Then WshNetwork.SetDefaultPrinter FullPrinterString
End Function
 
Public Function ReturnComputerDescription (sCpuname)
	On Error Resume Next
	Set objComputer = GetObject(QueryActiveDirectory("sCpuname"))
	objProperty = objComputer.Get("Description")
	If IsNull(objProperty) Then
	    ReturnComputerDescription = "The description has not been set."
	Else
	    WReturnComputerDescription = objProperty
	    objProperty = Null
	End If
End Function	
 
Public Function QueryActiveDirectory(sCpuname)
'Function:      QueryActiveDirectory
'Purpose:       Search the Active Directory's Global Catalog for users
'Parameters:    UserName - user to search for
'Return:        The user's distinguished name
 
    Dim oAD 'As IADs
    Dim oGlobalCatalog 'As IADs
    Dim oRecordSet 'As Recordset
    Dim oConnection 'As New Connection
    Dim strADsPath 'As String
    Dim strQuery 'As String
    Dim strUPN 'As String
 
    Set oRecordSet = CreateObject("ADODB.Recordset")
    Set oConnection = CreateObject("ADODB.Connection")
 
    'Determine the global catalog path
    Set oAD = GetObject("GC:")
    For Each oGlobalCatalog In oAD
        strADsPath = oGlobalCatalog.ADsPath
    Next
    'Initialize the ADO object
    oConnection.Provider = "ADsDSOObject"
    'The ADSI OLE-DB provider
    oConnection.connectiontimeout = 15
    oConnection.Open "ADs Provider"
    'Create the search string
    'MsgBox strADsPath
    strQuery = "<" & strADsPath & _
      ">;(&(objectClass=computer)(Name=" & _
      sCpuname & "));location,cn,distinguishedName;subtree"
        'Execute the query
    'MsgBox strQuery
    Set oRecordSet = oConnection.Execute(strQuery)
    If oRecordSet.EOF And oRecordSet.BOF Then
       'An empty recordset was returned
        QueryActiveDirectory = "Not Found"
    Else    'Records were found; loop through them
        While Not oRecordSet.EOF
            QueryActiveDirectory = oRecordSet.Fields("distinguishedName")
            oRecordSet.MoveNext
        Wend
    End If
    oConnection.Close
    Set oConnection = Nothing
    Set oRecordSet = Nothing
End Function

Open in new window

0
 
LVL 3

Expert Comment

by:Angelized
ID: 22841415
Btw : how to delete printer
Function RemovePrinter (sPrintServer,sPrinter)
   Set WshNetwork = CreateObject("WScript.Network")
   Dim FullPrinterString
   FullPrinterString = "\\" & sPrintServer & "\" &  sPrinter
   WshNetwork.RemovePrinterConnection FullPrinterString   
End Function

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question