I have a working powershell script for creating printers on Windows 7 workstations. I'm trying to update the same script by adding a GUI that will allow me to select and create multiple printers. My script works fine when selecting a single printer from the list, but when I choose multiple printers it always hangs on creation of the first printer on $Printer.Put(). The first printer is created correctly and seems to work without any problem.
I'm new to powershell so I'd also welcome any suggestions on general improvements that I could make. Thanks for your help.
Andy
$SelectedPrinters = ""
$result = ""
$PrinterName = ""
$PrinterIP = ""
$DriverName = ""
$PortName = ""
$Arch = (gwmi win32_computersystem).SystemType
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = ""
$form.Size = New-Object System.Drawing.Size(171,280)
$form.StartPosition = "CenterScreen"
# OK Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,210)
$OKButton.Size = New-Object System.Drawing.Size(65,23)
$OKButton.Text = "Install"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
# Cancel button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(81,210)
$CancelButton.Size = New-Object System.Drawing.Size(65,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
# Text
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,10)
$label.Size = New-Object System.Drawing.Size(135,20)
$label.Text = "Select printer(s) to install:"
$form.Controls.Add($label)
# List
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,30)
$listBox.Size = New-Object System.Drawing.Size(135,20)
$listBox.SelectionMode = "MultiExtended"
[void] $listBox.Items.Add("Milton 002")
[void] $listBox.Items.Add("Milton 106")
[void] $listBox.Items.Add("Milton 205")
[void] $listBox.Items.Add("Milton 245")
[void] $listBox.Items.Add("Milton 246")
[void] $listBox.Items.Add("Milton 323")
[void] $listBox.Items.Add("Milton 325")
[void] $listBox.Items.Add("Milton 328")
[void] $listBox.Items.Add("Milton 328")
[void] $listBox.Items.Add("Milton 335")
[void] $listBox.Items.Add("Milton 405")
[void] $listBox.Items.Add("Milton 434")
[void] $listBox.Items.Add("Summer 180e")
[void] $listBox.Items.Add("Center")
$listBox.Height = 185
$form.Controls.Add($listBox)
$form.Topmost = $True
$result = $form.ShowDialog()
$SelectedPrinters = $listBox.SelectedItems
IF ((($result -eq [System.Windows.Forms.DialogResult]::OK) -and ($SelectedPrinters -ne "")))
{
# *******************
Write-Host $SelectedPrinters
foreach ($item in $SelectedPrinters)
{
switch ($item) {
"Milton 002" {
$PrinterName = "Ricoh MP 5001 Milton 002"
$PrinterIP = "192.168.1.56"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.56 MILT 002"
}
"Milton 106" {
$PrinterName = "Ricoh MP 5001 Milton 106"
$PrinterIP = "192.168.1.21"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.21 MILT 106"
}
"Milton 205" {
$PrinterName = "Ricoh MP 5001 Milton 205"
$PrinterIP = "192.168.1.22"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.22 MILT 205"
}
"Milton 245" {
$PrinterName = "Ricoh MP 5001 Milton 245"
$PrinterIP = "192.168.1.24"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.24 MILT 245"
}
"Milton 246" {
$PrinterName = "Ricoh MP 5001 Milton 246"
$PrinterIP = "192.168.1.23"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.23 MILT 246"
}
"Milton 323" {
$PrinterName = "Ricoh MP 5001 Milton 323"
$PrinterIP = "192.168.1.26"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.26 MILT 323"
}
"Milton 325" {
$PrinterName = "Ricoh MP 5001 Milton 325"
$PrinterIP = "192.168.1.27"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.27 MILT 325"
}
"Milton 335" {
$PrinterName = "Ricoh MP 5001 Milton 335"
$PrinterIP = "192.168.1.25"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.25 MILT 335"
}
"Milton 328" {
$PrinterName = "Ricoh MP C550EX Milton 328"
$PrinterIP = "192.168.1.29"
$DriverName = "E-8100 PS V1.1 US"
$PortName = "192.168.1.29 MILT 328 C550EX"
}
"Milton 328" {
$PrinterName = "Ricoh MP C5000 Milton 328"
$PrinterIP = "192.168.1.28"
$DriverName = "RICOH Aficio MP C5000 PCL 6"
$PortName = "192.168.1.28 MILT 328 C5000"
}
"Milton 405" {
$PrinterName = "Ricoh MP 5001 Milton 405"
$PrinterIP = "192.168.1.30"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.30 MILT 405"
}
"Milton 434" {
$PrinterName = "Ricoh MP 5001 Milton 434"
$PrinterIP = "192.168.1.31"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "192.168.1.31 MILT 434"
}
"Summer 180e" {
$PrinterName = "Ricoh MP 5001 Summer 180e"
$PrinterIP = "139.78.22.35"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "139.78.22.35 SUM 180e"
}
"Flight Center" {
$PrinterName = "Ricoh MP 5001 Center"
$PrinterIP = "139.78.6.129"
$DriverName = "Ricoh Aficio MP 5001 PCL 6"
$PortName = "139.78.6.129 CEN"
}
default {
$PrinterName = "Invalid printer selected"
}
}
Write-Host "Installing the driver..."
If ($Arch.Contains("86") -eq $False) {
if($PrinterName.Contains("5001") -eq $True) {
Start-Process "RunDll32" -ArgumentList 'printui.dll, PrintUIEntry /ia /m "Ricoh Aficio MP 5001 PCL 6" /h "x64" /v "Type 3 - User Mode" /f "\\tuels.educn.springf.edu\tech\Ricoh\MP5001 Drivers\Windows x64\OEMSETUP.INF"' -Wait
}
elseif ($PrinterName.Contains("C550EX") -eq $True) {
Start-Process "RunDll32" -ArgumentList 'printui.dll, PrintUIEntry /ia /m "E-8100 PS V1.1 US" /h "x64" /v "Type 3 - User Mode" /f "\\tuels.educn.springf.edu\tech\Ricoh\C550EX Drivers\Windows\OEMSETUP.INF"' -Wait
}
else {
Start-Process "RunDll32" -ArgumentList 'printui.dll, PrintUIEntry /ia /m "RICOH Aficio MP C5000 PCL 6" /h "x64" /v "Type 3 - User Mode" /f "\\tuels.educn.springf.edu\tech\Ricoh\MPC5000 Drivers\Windows x64\OEMSETUP.INF"' -Wait
}
}
Else {
if($PrinterName.Contains("5001") -eq $True) {
Start-Process "RunDll32" -ArgumentList 'printui.dll, PrintUIEntry /ia /m "Ricoh Aficio MP 5001 PCL 6" /h "x86" /v "Type 3 - User Mode" /f "\\tuels.educn.springf.edu\tech\Ricoh\MP5001 Drivers\Windows x86\OEMSETUP.INF"' -Wait
}
elseif ($PrinterName.Contains("C550EX") -eq $True) {
Start-Process "RunDll32" -ArgumentList 'printui.dll, PrintUIEntry /ia /m "E-8100 PS V1.1 US" /h "x86" /v "Type 3 - User Mode" /f "\\tuels.educn.springf.edu\tech\Ricoh\C550EX Drivers\Windows\OEMSETUP.INF"' -Wait
}
else {
Start-Process "RunDll32" -ArgumentList 'printui.dll, PrintUIEntry /ia /m "RICOH Aficio MP C5000 PCL 6" /h "x86" /v "Type 3 - User Mode" /f "\\tuels.educn.springf.edu\tech\Ricoh\MPC5000 Drivers\Windows x86\OEMSETUP.INF"' -Wait
}
}
Write-Host "Installing the driver... !DONE"
Write-Host ""
##### Create the printer port #
Write-Host "Creating the port..."
$Port = ([wmiclass]"win32_tcpipprinterport").createinstance()
$Port.Name = $PortName
$Port.HostAddress = $PrinterIP
$Port.Protocol = "1"
$Port.PortNumber = "9100"
$Port.SNMPEnabled = $false
$Port.Description = $PortName
$Port.Put()
Write-Host "Creating the port... !DONE"
Write-Host ""
##### Creating The Printer #
Write-Host "Creating the printer..."
$Printer = ([wmiclass]"win32_Printer").createinstance()
$Printer.Name = $PrinterName
$Printer.DriverName = $DriverName
$Printer.DeviceID = $PrinterName
$Printer.Shared = $false
$Printer.PortName = $PortName
$Printer.Put()
Write-Host "Creating the printer... !DONE"
Write-Host ""
}
}
ELSEIF ((($result -eq [System.Windows.Forms.DialogResult]::OK) -and ($SelectedPrinters -eq $null))) {Write-Host "No printers selected"}
ELSEIF ($result -eq [System.Windows.Forms.DialogResult]::CANCEL) {Write-Host "Operation canceled by user"}
# Windows 8
#Add-PrinterPort -name $PortName -PrinterHostAddress $PrinterIP -PortNumber 9100 -SNMP $False
#Add-Printer -name $PrinterName -drivername $DriverName -port $PortName -Shared $False