![如何获取远程计算机上的打印机列表?](https://linux22.com/image/1440475/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E8%BF%9C%E7%A8%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%B8%8A%E7%9A%84%E6%89%93%E5%8D%B0%E6%9C%BA%E5%88%97%E8%A1%A8%EF%BC%9F.png)
我正在使用它来尝试获取远程计算机上的打印机列表:
Get-WmiObject win32_printer -ComputerName "$oldPcName"
问题是我只能获取本地打印机,而无法获取连接到计算机的打印服务器上的打印机。如何获取网络打印机列表?
我的目标是获取远程计算机上的网络打印机列表,删除它们,并从不同的打印服务器添加不同的打印机。
答案1
在 Windows 7 上
要查看联网打印机,我需要阅读注册表
Function InstalledPrinters ($ComputerName)
{
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
$InstalledPrinters = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Connections"
$InstalledPrinters | Add-Member -Name 'PrinterName' -MemberType NoteProperty -Value ""
Foreach ($InstalledPrinter in $InstalledPrinters) {$InstalledPrinter.PrinterName = $InstalledPrinter.GetValue("Printer").split("\")[3]}
Return $InstalledPrinters | sort PrinterName | select PSComputerName, PrinterName
}
}
要删除网络打印机:
rundll32.exe PRINTUI.DLL PrintUIEntry /gd /c\\$ComputerName /n\\$PrintServer\$PrinterName Gw /q
要安装网络打印机:
rundll32.exe PRINTUI.DLL PrintUIEntry /ga /c\\$ComputerName /n\\$PrintServer\$PrinterName Gw /q
答案2
这个老问题被社区机器人提出来了——真正的答案是最多网络打印机(通过打印服务器连接或由另一台计算机共享)配置在用户会话级别,而不是整个设备。
Powershell 可以通过多种方式连接,例如 PS 远程处理或 WMI,但根据设计,它将始终以您自己的帐户运行。这意味着当您或您的脚本以管理员用户身份连接时,您只会看到该帐户连接的打印机。
为了删除用户的网络打印机,您必须运行脚本作为受影响的用户。 对此的一些选项包括:
- 组策略:在用户配置 > 首选项 > 控制面板设置 > 打印机下
- 可以在用户环境中运行的远程管理工具(例如 SCCM 或 Intune)
- 用户登录脚本
解决这个问题的唯一方法是更复杂的过程加载另一个用户的注册表配置单元
答案3
#--------------------------
#Set Execution Of PSScripts
#--------------------------
Set-ExecutionPolicy Unrestricted -force
#------------
#Turn Off UAC
#------------
New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\policies\system -Name EnableLUA -PropertyType DWord -Value 0 -Force
#------------------------------
#Enter The Name Of The Computer
#------------------------------
$comp = "Name of computer"
#or if you wish to be prompted for the computer name
$comp = Read-host 'Enter the name of the computer?'
#---------------------------------------
#Starts WinRM Service On Remote Computer
#---------------------------------------
Import-Module Remote_PSRemoting -force
Set-WinRMListener -computername $comp
Restart-WinRM -computername $comp
Set-WinRMStartUp -computername $comp
Start-Sleep -Seconds 60
#----------------------------------------------
#Establish a PSSession With The Remote Computer
#----------------------------------------------
New-PSSession $comp | Enter-PSSession
#All of the replace commands are used to strip the extra characters and just #give a \\server\printer path return
#-----------------------
#Gets A List Of Printers
#-----------------------
$printers1 = Get-childitem -Path HKCU:\printers\connections | select name
$printers2 = $printers1 -replace '.*,,'
$printers3 = $printers2 -replace ',','\'
$printers = $printers3 -replace '}', ''
------------------------------------------------------
#To Replace The Old Print Server Name With The New One
------------------------------------------------------
$newprinters = $printers -replace 'oldserver','\\newserver'
#--------------------
#Gets Default Printer
#--------------------
$default = Get-itemproperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows" | select device
$default1 = $default -replace '.*='
$default2 = $default1 -replace '()'
$default3 = $default2 -replace ',winspool'
$defaultprinter = $default3 -replace ',.*'
------------------------------------------------------
#To Replace The Old Print Server Name With The New One
------------------------------------------------------
$newdefaultprinter = $defaultprinter -replace 'oldserver','\\newserver'
#------------------------
#Deletes The Old Printers
#------------------------
Get-WMIObject Win32_Printer | where{$_.Network -eq 'true'} | foreach{$_.delete()}
#----------------------------------------
#Exits PSSession With The Remote Computer
#----------------------------------------
Exit-PSSession
#-----------
#Turn UAC On
#-----------
#Value = 0 through 4 depending on the level of UAC
New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\policies\system -Name ConsentPromptBehaviorAdmin -PropertyType DWord -Value 2 -Force
#------------------------------------
#Turn Off Execution Policy Of Scripts
#------------------------------------
Set-ExecutionPolicy undefined -Force
#####This is as far as I could get with it. I always turn off UAC and Enable Scripts in the beginning and turn them back on ant the end. The summary of this script will give you the new network Printer paths and the users default printers. It also deletes the users old network printers. With powershell versions before windows 8 and server 2012, you would have to create a logon script to add the new printers and mark the default printer using WMI commands. Use could also use a csv file with a list of computer names as an input if you wish to run this command on multiple computers. It would look something like...
$csv = Import-csv -Path pathofcsvfile
foreach ($line in $csv) {
#With a bracket at the end to run through each computer in the list...
由于 Windows 的新版本具有 cmdlet,因此这一切都变得更加容易Get-printers
...
希望这可以帮你开始...我很想看到有人完成这个脚本,因为我没有时间在工作中这样做...