2016-05-09 2 views
2

Ich mache ein Skript, das den Endbenutzer mit Druckerproblemen unterstützt. An einem Punkt muss der Benutzer in der Lage sein, aus einer Liste von Druckern im Netzwerk auszuwählen, um zu entscheiden, welche Wartung benötigt wird. Ich versuche, dies über eine Listbox zu integrieren, aber ich konnte es nicht wirklich zum Funktionieren bringen. Hier ist das Skript, wie es steht (es nutzt derzeit Lese Host, damit der Benutzer einen Druckernamen eingeben.)Wie kann ich dem Benutzer eine Liste von Druckern zur Auswahl in meinem PowerShell-Skript zur Verfügung stellen?

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) 
} 

Hier ist die „Listenfeld“ Code, den ich implementieren möchten.

[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() 

I versucht, die hart codierte Werte mit $objListBox.Items.Add(Get-WMIObject -Class Win32_Printer | Select Name | ft -auto) ersetzt

aber wmi.object [] ist alles, was in der Liste erscheint. Was mache ich falsch?

+1

Sie müssen nicht nur die Name-Eigenschaft auswählen, Sie müssen es erweitern und dann die "ft-auto" loswerden. Also etwas wie 'Get-WMIObject -Klasse Win32_Printer | Wählen Sie -Expand Name |% {$ objListBox.Items.Add ($ _)} '... ja, das würde es tun. Rufen Sie eine Druckerliste auf, erweitern Sie die Eigenschaft Name, und fügen Sie für jeden Namen das Element zur List hinzu. – TheMadTechnician

Antwort

3

Die Verwendung von Formularen ist sehr schwer für so etwas.

My recommendation is to use Out-GridView.

Es bietet eine sortierbare, durchsuchbare, filterbare Liste in einem Popup-Fenster. Darüber hinaus kann der Benutzer ein oder mehrere Elemente auswählen, die zur Ausgabe des Cmdlets werden.

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

Ich werde es versuchen und wiederkommen, danke! –