Solved

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

Posted on 2008-10-28
2
418 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

726 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