【发布时间】:2020-03-18 15:45:16
【问题描述】:
我正在编写一个脚本,让用户可以更轻松地添加网络打印机。使用“listBox”,它会显示我的打印服务器中所有已配置的打印机,然后按我的“添加打印机”按钮,它会自动添加。
现在我想做第二个按钮,它将列表中选择的设备设置为默认打印机。
我只知道 SetDefaultPrinter 命令,但我认为那不是正确的。
这是我的代码:
#window
$window = New-Object System.Windows.Forms.Form
$window.Text = 'Select a Printer'
$window.Size = New-Object System.Drawing.Size(500, 400)
$window.StartPosition = 'CenterScreen'
#okButton
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(340,130)
$okButton.Size = New-Object System.Drawing.Size(96,48)
$okButton.Text = 'Drucker installieren'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$window.AcceptButton = $okButton
#favoriteButton
$favoriteButton = New-Object System.Windows.Forms.Button
$favoriteButton.Location = New-Object System.Drawing.Point(340,240)
$favoriteButton.Size = New-Object System.Drawing.Size(96,48)
$favoriteButton.Text = 'Als Standarddrucker festlegen'
$favoriteButton_Click{
}
#Label
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select a printer'
#ListBox
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,60)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height= 280
Get-Printer -ComputerName srvpr01 | Sort-Object | ForEach-Object { $listBox.Items.Add($_.Name) }
$window.TopMost = $true
$window.Controls.Add($listBox)
$window.controls.Add($label)
$window.Controls.Add($favoriteButton)
$window.Controls.Add($okButton)
$result = $window.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK){
$x = $listBox.SelectedItem
Add-Printer -ConnectionName \\srvpr01\$x
}
感谢任何类型的帮助和反馈!
【问题讨论】:
标签: powershell add printers network-printers