Get-Printer并将输出设置为可用于设置默认打印机的变量

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (4)

我目前正在获取计算机所连接的所有打印机的列表。我希望打印机成为列表的一部分,然后用户可以选择将哪台打印机设置为默认值。然后它将执行我已经创建的脚本的下一部分,然后创建一个文档然后打印。

Get-Printer | Select PortName | Select-String -Pattern "SM"

这就是我希望代码返回的内容


1. SM04
2. SM05
3. SM06
4. SM10

Please select a printer from the list: 3

Default printer has been set to: SM06. 

任何帮助将非常感激,试图让这个工作。

提问于
用户回答回答于

感谢js2010和LotPings,我能够弄明白。我甚至能够得到计算机所连接的所有打印机的列表!

这是代码,以防其他人试图做这样的事情。

#Asks the user if they would like to switch the default printer
$defaultPrinter1 = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" | select ShareName
$defaultPrinter1 = ("$defaultPrinter1").substring(12,4)
Write-Host "Barcodes will be printed at your default Printer. Your default printer is currently: $defaultPrinter1"
$printerConf = Read-Host "Would you like to change the default printer? Y/N"


#Switches dafault printer to the one specified by the user. 
if ($printerConf -eq "Y") 
{
    Write-Host "Here is list of connected printers`n" 
    $printerList = Get-Printer | Select PortName | Select-String -Pattern "SM" 
    $printerList-replace ".*=" -replace "}.*"
    $wshNet = New-Object -ComObject WScript.Network
    $selectPrinter = Read-Host "`nWhat is the printers name?"
    $wshNet.SetDefaultPrinter($selectPrinter)

    $defaultPrinter2 = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" | select ShareName 
    $defaultPrinter2 = ("$defaultPrinter2").substring(12,4)  
    Write-Host "Default Printer has been changed. It is now: $defaultPrinter2"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
}


用户回答回答于

Get-WmiObject用来获取/设置默认打印机,并 Out-Gridview使用-PassThru参数在GUI中选择新的默认值

## Q:\Test\2019\05\20\SO_56222678.ps1
# Save old default Printer
$OldDefaultPrinter = (Get-WmiObject -Namespace root\cimv2 -Query `
    "select * from Win32_Printer Where Default = TRUE" -Impersonation 3).Name

# Get-Printer matching pattern and select in Out-Gridview
$NewDefaultPrinter = (Get-Printer | Where-Object Portname -like 'SM*'| 
  Select-Object Name,Portname |
    Out-Gridview -Title 'Select a printer' -PassThru).Name

# Set selected as new default
$Null = (Get-WmiObject -Class Win32_Printer -Filter "Name='$NewDefaultPrinter'").SetDefaultPrinter()

# Check new default is set
(Get-WmiObject -Namespace root\cimv2 -Query `
    "select * from Win32_Printer Where Default = TRUE" -Impersonation 3).Name

扫码关注云+社区

领取腾讯云代金券