在 powershell 中将网络打印机设置为默认打印机
Set a network printer as the default printer in powershell
我正在编写一个脚本,使用户可以更轻松地添加网络打印机。使用 "listBox",它会显示我的打印服务器中所有已配置的打印机,按我的 "add printer"- 按钮,它会自动添加。
现在我想做第二个按钮,将列表中的选定设备设置为默认打印机。
我只知道 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
}
感谢任何类型的帮助和反馈!
这里记录了两种方法,使用 wmi 或 com 对象。
每个用户都必须是 运行。
https://docs.microsoft.com/en-us/powershell/scripting/samples/working-with-printers?view=powershell-7#setting-a-default-printer
我正在编写一个脚本,使用户可以更轻松地添加网络打印机。使用 "listBox",它会显示我的打印服务器中所有已配置的打印机,按我的 "add printer"- 按钮,它会自动添加。
现在我想做第二个按钮,将列表中的选定设备设置为默认打印机。
我只知道 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
}
感谢任何类型的帮助和反馈!
这里记录了两种方法,使用 wmi 或 com 对象。 每个用户都必须是 运行。 https://docs.microsoft.com/en-us/powershell/scripting/samples/working-with-printers?view=powershell-7#setting-a-default-printer