?
Solved

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

Posted on 2008-10-28
2
Medium Priority
?
424 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
[X]
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
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
Angelized earned 1000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans

762 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