如何在Windows中使用IP地址更改网络打印机名称

elcex8rz  于 2023-01-27  发布在  Windows
关注(0)|答案(1)|浏览(252)

我只是想在这里发布我的解决方案,以备大家需要。我们最近不得不更换一台在组织中广泛使用的打印机。我们正在使用这台打印机的通用驱动程序,只需要更新机器上打印机的名称。下面是我编写的powershell脚本。

#Pulls printer name, but it is formatted and not clean. Replace IP with printer IP
$PrinterExtract = wmic printer where "PortName LIKE 'IP%'" GET Name 

#removes 'name' string from variable
$PrinterExtract = $PrinterExtract -replace 'Name',''

#removes all empty lines and formatting and creates a new variable
$PrinterName = ($PrinterExtract | Where { $_ -ne "" } | Out-String).Trim()

#pulls printer object
$PrinterObject = Get-Printer -Name $PrinterName

#renames printer. Replace new printer name with the name of new printer
Rename-Printer -InputObject $PrinterObject -NewName "New Printer Name"

我试图不使用wmic来执行这个任务,但是我找不到一个方法来从IP地址中提取现有的打印机名称。如果有人能提供一个更现代化的解决方案,我将不胜感激。

pexxcrt2

pexxcrt21#

我想到了如果有人需要

#Pulls printer name, but it is formatted and not clean
    $PrinterExtract = (Get-Printer | Where-Object {$_.PortName -Like "IP"} | Select Name | Where {$_ -ne ""} | Out-String).Trim()
    
    #removes 'name' string from variable
    $PrinterExtract = $PrinterExtract -replace 'Name',''
    
    #removes '----' string from variable
    $PrinterExtract = $PrinterExtract -replace '----',''
    
    #removes formatting and empty lines
    $PrinterName = ($PrinterExtract | Where { $_ -ne "" } | Out-String).Trim()
    
    #pulls printer object into new variable
    $PrinterObject = Get-Printer -Name $PrinterName
    
    #renames printer
    Rename-Printer -InputObject $PrinterObject -NewName "Printer Name"

相关问题