3

Im able to create and install printers using powershell. Now i also need to automate the printer configuration and need to change multiple values in the Administration tab.

Printer Settings

How can i do that via powershell? I tried Set-PrinterProperty but i can't get it to work.

Thanks

2 Answers2

4

One way I found easy to implement this was to start from current printer configuration, using Get-PrinterConiguration, then look at the xml and change whatever you need to, then use Set-PrinterProperty to push up the new xml.

Below is a function I created a while ago to update Printer Tray. It should (hopefully) get you started.

Function Set-MyDefaultPrinterTray {
#Requires -module PrintManagement
<#
    .SYNOPSIS
    Update Default Tray assignment of printer

    .EXAMPLE
    > Set-MyDefaultPrinterTray -ComputerName (Get-Content C:\temp\epicprinter\servers.txt) -PrintQueue ZZZ_Adil_Test03 -Tray 4 -Verbose
    VERBOSE: Change tray to Tray4 on epswcdcqvm001
    VERBOSE: Getting PrintConfiguration...
    VERBOSE: epswcdcqvm001 : CurrentTray is psk:AutoSelect
    VERBOSE: epswcdcqvm001 : New Tray ns0000:Tray4
    VERBOSE: Performing the operation "Update Tray" on target "epswcdcqvm001".
    VERBOSE: epswcdcqvm001 : Setting new tray assignment
    VERBOSE: epswcdcqvm001 : Adding to success table
    VERBOSE: Change tray to Tray4 on epswcdcqvm002
    VERBOSE: Getting PrintConfiguration...
    VERBOSE: epswcdcqvm002 : CurrentTray is psk:AutoSelect
    VERBOSE: epswcdcqvm002 : New Tray ns0000:Tray4
    VERBOSE: Performing the operation "Update Tray" on target "epswcdcqvm002".
    VERBOSE: epswcdcqvm002 : Setting new tray assignment
    VERBOSE: epswcdcqvm002 : Adding to success table
    VERBOSE: Change tray to Tray4 on epswcdcqvm001
    VERBOSE: Getting PrintConfiguration...
    VERBOSE: epswcdcqvm001 : CurrentTray is ns0000:Tray4
    VERBOSE: epswcdcqvm001 : New Tray ns0000:Tray4
    VERBOSE: Performing the operation "Update Tray" on target "epswcdcqvm001".
    VERBOSE: epswcdcqvm001 : Setting new tray assignment

    Name                           Value
    ----                           -----
    epswcdcqvm002                  Succeed
    epswcdcqvm001                  Succeed

    .EXAMPLE
    D:\> Set-MyDefaultPrinterTray -PrintServer 'epswcdcqvm001','epswcdcqvm002' -PrintQueue ZZZ_Adil_Test03 -Tray Tray2 -Verbose
    VERBOSE: Change tray to Tray2 on epswcdcqvm001
    VERBOSE: Getting PrintConfiguration...
    VERBOSE: epswcdcqvm001 : CurrentTray is psk:AutoSelect
    VERBOSE: epswcdcqvm001 : New Tray ns0000:Tray2
    VERBOSE: Performing the operation "Set-EpicDefaultPrinterTray" on target "epswcdcqvm001".
    VERBOSE: epswcdcqvm001 : Setting new tray assignment
    VERBOSE: Change tray to Tray2 on epswcdcqvm002
    VERBOSE: Getting PrintConfiguration...
    VERBOSE: epswcdcqvm002 : CurrentTray is psk:AutoSelect
    VERBOSE: epswcdcqvm002 : New Tray ns0000:Tray2
    VERBOSE: Performing the operation "Set-EpicDefaultPrinterTray" on target "epswcdcqvm002".
    VERBOSE: epswcdcqvm002 : Setting new tray assignment
#>
    [CMDLETBINDING(SupportsShouldProcess)]
    param(            
            [Parameter(Mandatory,ValueFromPipeline,Position=0)]
            [Alias('PrintServer')]                
            [string[]]$ComputerName,
            #[string[]]$PrintServer,

            [Parameter(Mandatory,Position=1)]
            [string]$PrintQueue,

            [ValidateSet('1','2','3','4','Tray1','Tray2','Tray3','Tray4','AutoSelect','ManualFeed')]
            $Tray='AutoSelect'
        )    
    BEGIN 
    {
           switch ($tray)  
           {
             1  {$tray='Tray1';break}
             2  {$tray='Tray2';break}
             3  {$tray='Tray3';break}
             4  {$tray='Tray4';break}
           }

           $result = @{}
    }
    PROCESS 
    {

        Foreach ($ps in $ComputerName)
        {
            Write-Verbose "Change tray to $tray on $ps"  

            try 
            { 
                if (! (Test-Connection -ComputerName $ps -Count 1 -Quiet)) {
                    throw "Not Pingable"                        
                }

                Write-Verbose "Getting PrintConfiguration..."
                $PrintConfiguration = Get-PrintConfiguration -ComputerName $ps -PrinterName $PrintQueue
                $PrintTicketXML = [xml]$PrintConfiguration.PrintTicketXML

                $currentTray = ($PrintTicketXML.PrintTicket.Feature).where({$_.name -eq 'psk:JobInputBin'}).option.name
                Write-Verbose "$ps : CurrentTray is $currentTray "


                if ($Tray -eq 'AutoSelect') {                                        
                    $NewTray= "psk:$Tray"
                } else {
                    $NewTray= "ns0000:$Tray"
                }

                Write-Verbose "$ps : New Tray $NewTray "

                $UpdatedPrintTicketXML = $PrintConfiguration.PrintTicketXML -replace "$currentTray","$NewTray"


                if ($PSCmdlet.ShouldProcess($ps,"Update Tray")  ) {
                    Write-Verbose "$ps : Setting new tray assignment"
                    Set-PrintConfiguration -ComputerName $ps -printername $PrintQueue -PrintTicketXml $UpdatedPrintTicketXML
                   if (!$result.ContainsKey($ps)) { 
                        Write-Verbose "$ps : Adding to success table"
                        $result.Add($ps,'Succeed')
                    }
                }

            }
            catch 
            {
                    if (!$result.ContainsKey($ps)) { 
                        Write-Verbose "$ps : Adding to fail table"
                        $result.Add($ps,'Fail')
                    }

                Write-Error $error[0].exception
            }

        }
    }
    END 
    {
           $result
    }

}
Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28
  • Ok i was able to get the features, but none correspond to the Administration Tab. Here the options i got: http://pastebin.com/8NsSU2za – Tiago Conceição Nov 08 '15 at 14:52
  • Unfortunately, not everything is available for you to manage this way. What's possible is documented on the help. I don't have a way to test but take a look at Get-PrinterProperty and Set-PrinterProperty to see if the property changes you want are available there: https://technet.microsoft.com/en-us/library/hh918351(v=wps.630).aspx – Adil Hindistan Nov 09 '15 at 03:11
  • I know... About the Set-PrinterProperty function how can i know the properties names? How can i get a complete list? I tried blind names but error is returned :( – Tiago Conceição Nov 10 '15 at 00:10
  • You can use Get-PrinterProperty for that. Try: get-printer * | %{get-printerproperty $_.name} and look at the config:* properties. Those are the ones you can set using Set-PrinterProperty – Adil Hindistan Nov 10 '15 at 02:18
  • these commands look like magic xD thanks i will try it latter :) – Tiago Conceição Nov 10 '15 at 16:58
  • humm only "Config:Memory String M264" is showing. Is that normal? – Tiago Conceição Nov 10 '15 at 17:02
3

Best way to do this is to use the below Windows tool, it should, but of course it isn't guaranteed, work with the "Administration tab".

The way this tool works is, you firstly set the printer with the settings you want (configure your Administration tab accordingly) and export the settings to a file with a command like this (in CMD or PowerShell):

RUNDLL32 PRINTUI.DLL,PrintUIEntry /Ss /n "PRINTER_NAME" /a "C:\printerSettings.dat" g d u

At "PRINTER_NAME" you enter your desired printer name (with quotation marks) and at "C:..." the location where the settings file should be saved. The parameters at the end here aren't necessarily, with the you specify what gets saved to the file, without any parameters everything gets saved and that might be best here...

Now, your settings are saved in a file, you'll then use that file for restoring settings on other printers with the same drivers with something like this:

RUNDLL32 PRINTUI.DLL,PrintUIEntry /Sr /n "PRINTER_NAME" /a "C:\printerSettings.dat" g d u p i r

The difference as you might noticed are the commands "/Ss" for saving and "/Sr" for restoring and different parameters at the end. Here a bit tricky thing to notice - if you run this with administration rights the above code should work fine, but otherwise you'll get a error. The problem is "g" parameter here, as it tries to change/restore the default settings of the printer and not only the settings for the current user. The settings for the current user are restored with "u". So you will maybe need to remove the "g" parameter.

Other parameters are described at the link, the more important ones I'll copy here (this are for restoring - "/Sr" command):

  • r: If the printer name stored in the file is different from the name of the printer being restored to, then use the current printer name. This cannot be specified with f. If neither r nor f is specified and the names do not match, restoration of the settings fails.
  • f: If the printer name stored in the file is different from the name of the printer being restored to, then use the printer name in the file. This cannot be specified with r. If neither f nor r is specified and the names do not match, restoration of the settings fails.
  • i: If the driver in the saved settings file does not match the driver for the printer being restored to, then the restoration fails.
  • p: If the port name in the file being restored from does not match the current port name of the printer being restored to, the printer’s current port name is used.
  • d: Use to restore printer specific data, such as the printer’s hardware ID.
Rok T.
  • 380
  • 5
  • 11