gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:29:51 25 4
gpt4 key购买 nike

我能够使用 powershell 创建和安装打印机。现在我还需要自动化打印机配置并需要更改“管理”选项卡中的多个值。

Printer Settings

我如何通过 powershell 做到这一点?我尝试了 Set-PrinterProperty,但无法正常工作。

谢谢

最佳答案

我发现一种易于实现的方法是从当前打印机配置开始,使用 Get-PrinterConiguration,然后查看 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
}

}

关于PowerShell 从“管理”选项卡更改打印机配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com