Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now