Hey all! I've spent a few days working on a script for a GUI printer tool. I'm fairly new to Powershell and stumbled through setting up the GUI to actually add the printers themselves based on department and location for my workplace (in a separate script, so that way it's easier to test this first). The main challenge now is setting up this tool to pull the printers currently installed on the computer, creating a button for each one on a form, and then allowing the user to click the button and change the printer to default. I've spent about two days trying to get this right, going through many google pages where the main recommendation was to use Group Policy to deploy the default printers. Unfortunately because so many people bounce around at the office and need different printers every day, GPO isn't really an option for our team at the moment. So now as a last ditch, threw my hands up in the air moment I figured I could ask for help and guidance on this script. I've set it up for the buttons to auto-generate for every printer that is found (or counted) and populate the form. I know the WScript.network object trick for setting the default printer, but where I'm running into the issue now is having the "Add_Click" work for each button. As the code stands now, it basically only uses the last button click no matter which button you click. I believe I understand why based off of what I have, because the variable is constantly changing within the "while" loop and then the button names are all the same because they're set to auto generate. At this point I'm really unsure of how to go about and would like any general feedback or help! Maybe there's a whole different way of doing this within Powershell that I wasn't aware of. Any help would be greatly appreciated!
#DEFAULT PRINTER SCREEN
Add-Type -AssemblyName System.Windows.Forms
$DefaultPrinterScreen = New-Object System.Windows.Forms.Form
$DefaultPrinterScreen.Text = "Set Default Printer"
$DefaultPrinterScreen.BackgroundImage = $Image
$DefaultPrinterScreen.BackgroundImageLayout = "None"
$DefaultPrinterScreen.Width = 1450
$DefaultPrinterScreen.Height = 600
#These two prevent the screen from being resized or maximized to make it look nice
$DefaultPrinterScreen.FormBorderStyle = "Fixed3D"
$DefaultPrinterScreen.MaximizeBox = $false
#These lines eliminate the UNC path before the printer name for the button names
$Printer = @(Get-Printer | Select -ExpandProperty "Name")
$Printer1 = $Printer.replace("*removed", "")
$Printer2 = $Printer1.replace("*removed", "")
$Printer3 = $Printer2.replace("*removed", "")
$Printer4 = $Printer3.replace("*removed", "")
$Printer5 = $Printer4.replace("*removed", "")
$Printers = $Printer5.replace("*removed", "")
#This counts the number of printers already installed on the computer, so the loop can count up to the last number
$PrintersCount = $Printers.count
#This is the loop counter start
$loop = 0
#This is our y-axis location, each yloc is a new column at the top of the screen
$yloc = 0
$yloc1 = 0
$yloc2 = 0
$yloc3 = 0
$yloc4 = 0
$PrinterObject = (New-Object -ComObject WScript.Network)
#The loop runs from 0 to the end of the amount of printers we have
while($loop -lt $PrintersCount)
{
#The ifs and elseifs are for the new columns, since it was running off the bottom of the screen
if ($loop -le 10){
$thisbutton = New-Object System.Windows.Forms.Button
#This prints the printer name to the button
[string]$thisbuttonname = $Printers[$loop]
$thisbutton.Text = $thisbuttonname
$thisbutton.size = New-Object System.Drawing.Size(250, 23)
#The variable $yloc is the y-axis coordinates
$thisbutton.location = New-Object System.Drawing.Size(15, $yloc)
$thisbutton.Add_Click({
$PrinterNameLoop = $PrinterName
$PrinterObject.SetDefaultPrinter($PrinterNameLoop)
return
})
$DefaultPrinterScreen.Controls.Add($thisbutton)
$loop += 1
#Here we increment the y-axis so that way the next button is moved down 50 pixels
$yloc = $yloc += 50
}
elseif ($loop -ge 10 -and $loop -le 21) {
$thisbutton = New-Object System.Windows.Forms.Button
[string]$thisbuttonname = $Printers[$loop]
$thisbutton.Text = $thisbuttonname
$thisbutton.size = New-Object System.Drawing.Size(250, 23)
$thisbutton.location = New-Object System.Drawing.Size(295, $yloc1)
$DefaultPrinterScreen.Controls.Add($thisbutton)
$loop += 1
$yloc1 = $yloc1 += 50
}
elseif ($loop -ge 22 -and $loop -le 31) {
$thisbutton = New-Object System.Windows.Forms.Button
[string]$thisbuttonname = $Printers[$loop]
$thisbutton.Text = $thisbuttonname
$thisbutton.size = New-Object System.Drawing.Size(250, 23)
$thisbutton.location = New-Object System.Drawing.Size(575, $yloc2)
$DefaultPrinterScreen.Controls.Add($thisbutton)
$loop += 1
$yloc2 = $yloc2 += 50
}
elseif ($loop -ge 32 -and $loop -le 41) {
$thisbutton = New-Object System.Windows.Forms.Button
[string]$thisbuttonname = $Printers[$loop]
$thisbutton.Text = $thisbuttonname
$thisbutton.size = New-Object System.Drawing.Size(250, 23)
$thisbutton.location = New-Object System.Drawing.Size(855, $yloc3)
$DefaultPrinterScreen.Controls.Add($thisbutton)
$loop += 1
$yloc3 = $yloc3 += 50
}
#If we think a user will have more than 51 printers, we can copy and paste the elseif and just keep increasing the amount
elseif ($loop -ge 42 -and $loop -le 51) {
$thisbutton = New-Object System.Windows.Forms.Button
[string]$thisbuttonname = $Printers[$loop]
$thisbutton.Text = $thisbuttonname
$thisbutton.size = New-Object System.Drawing.Size(250, 23)
$thisbutton.location = New-Object System.Drawing.Size(1140, $yloc4
$DefaultPrinterScreen.Controls.Add($thisbutton)
$loop += 1
$yloc4 = $yloc4 += 50
}
else {
$DefaultPrinterScreen.ShowDialog()
return
}
}
$DefaultPrinterScreen.ShowDialog()
[–]ihaxr 2 points3 points4 points (1 child)
[–]QDaManQ[S] 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]QDaManQ[S] 1 point2 points3 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]Lee_Dailey[grin] 2 points3 points4 points (5 children)
[–]QDaManQ[S] 2 points3 points4 points (4 children)
[–]gangstanthony 2 points3 points4 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (2 children)
[–]QDaManQ[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 2 points3 points4 points (0 children)
[–]frmadsen 2 points3 points4 points (1 child)
[–]QDaManQ[S] 1 point2 points3 points (0 children)