简体   繁体   English

PowerShell从“管理”选项卡更改打印机配置

[英]PowerShell change printer configuration from Administration tab

Im able to create and install printers using powershell. 我无法使用Powershell创建和安装打印机。 Now i also need to automate the printer configuration and need to change multiple values in the Administration tab. 现在,我还需要自动进行打印机配置,并需要在“管理”选项卡中更改多个值。

打印机设定

How can i do that via powershell? 我如何通过Powershell做到这一点? I tried Set-PrinterProperty but i can't get it to work. 我尝试了Set-PrinterProperty,但无法正常工作。

Thanks 谢谢

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. 我发现易于实现的一种方法是,从当前的打印机配置开始,使用Get-PrinterConiguration,然后查看xml并更改所需的内容,然后使用Set-PrinterProperty推送新的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
    }

}

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". 最好的方法是使用下面的Windows工具,该工具应该(但当然不能保证)与“管理”选项卡一起使用。

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): 该工具的工作方式是,首先为打印机设置所需的设置(相应地配置“管理”选项卡),然后使用以下命令(在CMD或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. 在“ PRINTER_NAME”中,输入所需的打印机名称(带引号),在“ C:...”中,输入保存设置文件的位置。 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. 您可能会注意到的区别是命令“ / Ss”用于保存,命令“ / Sr”用于恢复,最后还有不同的参数。 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. 问题是这里的“ g”参数,因为它试图更改/恢复打印机的默认设置,而不仅仅是当前用户的设置。 The settings for the current user are restored with "u". 当前用户的设置将用“ u”恢复。 So you will maybe need to remove the "g" parameter. 因此,您可能需要删除“ g”参数。

Other parameters are described at the link, the more important ones I'll copy here (this are for restoring - "/Sr" command): 链接中描述了其他参数,我将在此处复制更重要的参数(这是要还原的“ / Sr”命令):

  • 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. r:如果文件中存储的打印机名称与要还原到的打印机名称不同,请使用当前的打印机名称。 This cannot be specified with f. 不能用f指定。 If neither r nor f is specified and the names do not match, restoration of the settings fails. 如果未指定r或f并且名称不匹配,则恢复设置将失败。
  • 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. f:如果文件中存储的打印机名称与还原到的打印机名称不同,则使用文件中的打印机名称。 This cannot be specified with r. 不能用r指定。 If neither f nor r is specified and the names do not match, restoration of the settings fails. 如果未指定f或r并且名称不匹配,则恢复设置将失败。
  • i: If the driver in the saved settings file does not match the driver for the printer being restored to, then the restoration fails. i:如果保存的设置文件中的驱动程序与要还原到的打印机的驱动程序不匹配,则还原失败。
  • 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. p:如果要从中还原文件的端口名与要还原到的打印机的当前端口名不匹配,则使用打印机的当前端口名。
  • d: Use to restore printer specific data, such as the printer's hardware ID. d:用于还原打印机特定的数据,例如打印机的硬件ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在多个打印机上更改打印机驱动程序的PowerShell脚本 - PowerShell Script to Change Printer Drivers on Several Printers 通过Powershell更改打印机纸盒设置 - Change printer paper tray settings via powershell 从python运行使用Web管理模块的powershell脚本 - Run a powershell script from python that uses Web-Administration module 使用 PHP powershell 脚本更改默认打印机纸张尺寸 - Change Default Printer paper size with PHP powershell script 如何通过Powershell更改禁用的网络适配器配置 - how to change a disabled network adapter configuration by powershell Powershell + WPF XAML + Tabs-更改选项卡 - Powershell + wpf xaml + Tabs - change tab 在Powershell标签自动完成中更改过滤器方法 - Change filter approach in powershell tab auto complete 使用 powershell 自动配置 SSRS 2016(需要将 URL 从“http”的“https”更改) - Automating Configuration of SSRS 2016 with powershell (Need to change URL from "https" of "http") 从计算机PowerShell列表中获取打印机驱动程序列表 - Get List of Printer Drivers from list of Computers PowerShell 从 Powershell 使用 Microsoft.Web.Administration - 它连接到我的 IISExpress 实例而不是我的主要实例? - Using Microsoft.Web.Administration from Powershell - it's connecting to my IISExpress instance instead of my main one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM