如何在PowerShell脚本中向用户显示可供选择的打印机列表?

时间:2016-05-09 21:35:30

标签: windows powershell scripting

我制作的脚本可以帮助最终用户解决打印机问题。有一次,用户需要能够从网络上的打印机列表中进行选择,以决定哪一台需要维护。我试图通过列表框集成它,但我还没有能够让它运行起来。这是现有的脚本(它目前使用read-host来允许用户输入打印机名称。)

 net stop spooler

Remove-Item C:\Windows\System32\spool\PRINTERS\* -Force

net start spooler

get-printer

$PrinterName = Read-Host 'Please Type In The Name Of The Printer Above That You Are Having Problems With'

$PrinterInstance = [wmi]"\\.\root\cimv2:Win32_Printer.DeviceID='$PrinterName'"




try{
$PrinterInstance.PrintTestPage() 
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("I found a problem that I was able to fix.  Please try to print again.",0,"Printer Helper",0x1)
}
catch
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Printer Fixer can not solve your problem, please enter a new ticket.",0,"Printer Helper",0x1)
}

这是"列表框"我想要实现的代码。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please select a computer:"
$objForm.Controls.Add($objLabel) 

$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.Size = New-Object System.Drawing.Size(260,20) 
$objListBox.Height = 80

[void] $objListBox.Items.Add("atl-dc-001")
[void] $objListBox.Items.Add("atl-dc-002")
[void] $objListBox.Items.Add("atl-dc-003")
[void] $objListBox.Items.Add("atl-dc-004")
[void] $objListBox.Items.Add("atl-dc-005")
[void] $objListBox.Items.Add("atl-dc-006")
[void] $objListBox.Items.Add("atl-dc-007")

$objForm.Controls.Add($objListBox) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

我尝试用$objListBox.Items.Add(Get-WMIObject -Class Win32_Printer | Select Name | ft -auto)

替换硬编码值

但是wmi.object []就是列表框中显示的全部内容。我究竟做错了什么?

1 个答案:

答案 0 :(得分:3)

对于像这样的事情,使用表格是非常苛刻的。

My recommendation is to use Out-GridView

它在弹出窗口中提供可排序,可搜索,可过滤的列表。但它也允许用户选择一个或多个项目,这些项目将成为cmdlet的输出。

$printerSelection = $printerList | Out-GridView -OutputMode Single