$ACTIVE_PRINTERS = Get-Printer
try {
if (Get-Variable -Name LM -ErrorAction SilentlyContinue){
Write-Host "Lexmark Printers"
foreach ($PrinterName in $LM.Keys) {
$ip = $LM[$PrinterName]
if (!$ACTIVE_PRINTERS.Contains($PrinterName)) {
Add-PrinterPort "$ip"
Add-Printer -DriverName "Lexmark Universal v2" -Name $PrinterName -PortName "$ip"
}
Write-Host "$PrinterName : $ip Are Mapped"
}
} else {
Write-Host "No Lexmark Printers Located in $sqdrn"
}
} catch {
Write-Error "An error occurred: $($_.Exception.Message)"
}
我正在尝试编写一个脚本,该脚本可以遍历 IP 地址列表并映射打印机和名称,映射和命名方面可以工作,但过滤不起作用,它只是告诉我打印机:ip 对存在并出现错误。即使通过设置和控制面板删除/取消映射打印机。以下是位于 LM 数组中的键值对的示例。
$LM = @{"ROOM#1"="192.168.x.1";"ROOM#2"="192.168.x.2"}
以下是收到的错误。
Add-PrinterPort : The specified port already exists.
At C:\Users\foobar\Desktop\MappingPrinter.ps1:85 char:17
+ Add-PrinterPort "$ip"
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (MSFT_PrinterPortTasks:ROOT/StandardCimv2/MSFT_PrinterPortTasks) [Add-PrinterPort], CimException
+ FullyQualifiedErrorId : HRESULT 0x800700b7,Add-PrinterPort
Add-Printer : The specified printer already exists.
At C:\Users\foobar\Desktop\MappingPrinter.ps1:86 char:17
+ ... Add-Printer -DriverName "Lexmark Universal v2" -Name $Pri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_Printer:ROOT/StandardCimv2/MSFT_Printer) [Add-Printer], CimException
+ FullyQualifiedErrorId : HRESULT 0x8007070a,Add-Printer
我已经尝试了 $ACTIVE_PRINTERS 的 if 语句的许多变体,并且对 powershell 还很陌生。如果有人需要更多信息或者我没有提供足够的信息,请告诉我!