我能够使用 powershell 创建和安装打印机。现在我还需要自动化打印机配置,并且需要更改“管理”选项卡中的多个值。
我如何通过 powershell 做到这一点?我尝试了 Set-PrinterProperty 但无法让它工作。
谢谢
我发现实现这一点的一种简单方法是从当前打印机配置开始,使用 Get-PrinterConfiguration,然后查看 xml 并更改您需要的任何内容,然后使用 Set-PrinterProperty 推送新的 xml。
下面是我不久前创建的一个用于更新打印机托盘的函数。它应该(希望)帮助您开始。
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
}
}
最好的方法是使用下面的 Windows 工具,它应该(但当然不能保证)与“管理选项卡”一起使用。
此工具的工作方式是,您首先使用所需的设置来设置打印机(相应地配置“管理”选项卡),然后使用如下命令将设置导出到文件(在 CMD 或 PowerShell 中):
RUNDLL32 PRINTUI.DLL,PrintUIEntry /Ss /n "PRINTER_NAME" /a "C:\printerSettings.dat" g d u
在“PRINTER_NAME”处输入所需的打印机名称(带引号),并在“C:...”处输入应保存设置文件的位置。这里末尾的参数不一定,您可以指定保存到文件中的内容,没有任何参数,所有内容都会保存,这可能是最好的...
现在,您的设置保存在一个文件中,然后您将使用该文件恢复具有相同驱动程序的其他打印机上的设置,如下所示:
RUNDLL32 PRINTUI.DLL,PrintUIEntry /Sr /n "PRINTER_NAME" /a "C:\printerSettings.dat" g d u p i r
您可能注意到的区别是用于保存的命令“/Ss”和用于恢复的命令“/Sr”以及最后的不同参数。这里需要注意一点棘手的事情 - 如果您使用管理权限运行此代码,上面的代码应该可以正常工作,但否则您会收到错误。问题在于此处的“g”参数,因为它尝试更改/恢复打印机的默认设置,而不仅仅是当前用户的设置。使用“u”恢复当前用户的设置。因此,您可能需要删除“g”参数。
其他参数在链接中进行了描述,我将在此处复制更重要的参数(这是用于恢复 - “/Sr”命令):