我正在编写一个脚本,使用户更容易添加网络打印机。使用“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
}
我感谢任何类型的帮助和反馈!
2条答案
按热度按时间cuxqih211#
首先使用以下命令添加打印机:
然后使用以下命令将其设为默认值:
j5fpnvbx2#
这里有两种方法,使用wmi或com对象。它必须以每个用户的身份运行。https://learn.microsoft.com/en-us/powershell/scripting/samples/working-with-printers?view=powershell-7#setting-a-default-printer