Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Load-PrinterQueryGUI {
- #--//////////////////////-----
- #Purpose: Search Print server query across one or all print servers available.
- #Created: Gus Whitehouse
- #Description: This GUI is designed to assist tech staff provide front line support for printers and print servers.
- #-----
- #Inputs -->> Requires a CSV file containing a list of available print servers for quiery
- # > Uses the Get-Printer cmdlet available only in Windows 10
- # --> Support: https://technet.microsoft.com/en-us/library/hh918358.aspx
- # Makes use of the AddRecord function available in previous pastebin posts
- # --> http://pastebin.com/WW5CujK6
- Function AddRecord
- {
- [CmdletBinding()]
- Param
- (
- [Parameter(
- Mandatory=$true,
- ValueFromPipeline=$true)
- ][string]$Column1,
- [Parameter(
- Mandatory=$true,
- ValueFromPipeline=$true)
- ][string]$Column2,
- [Parameter(
- Mandatory=$true,
- ValueFromPipeline=$true)
- ][string]$Column3,
- [Parameter(
- Mandatory=$true,
- ValueFromPipeline=$true)
- ][string]$Column4,
- [Parameter(
- Mandatory=$false)
- ][string]$status
- )
- Begin{}
- Process
- {
- $rowitem = New-Object System.Windows.Forms.ListViewItem($Column1)
- Switch($status){
- "Normal"{$rowitem.ForeColor = [System.Drawing.Color]::FromArgb(255,0,100,0)}
- ("Offline","Error,Offline"){$rowitem.ForeColor = [System.Drawing.Color]::FromArgb(255,139,0,0)}
- "TonerLow"{$rowitem.ForeColor = [System.Drawing.Color]::FromArgb(255,244,164,96)}
- }
- $rowitem.SubItems.Add($Column2)
- $rowitem.SubItems.Add($Column3)
- $rowitem.SubItems.Add($Column4)
- $lstVDetails.Items.AddRange($rowitem)
- }
- End
- {
- $PrinterHeader.Width = -2
- $Serverheader.Width = -2
- $Portheader.Width = -2
- $Locationheader.Width = -2
- }
- }
- #region Import the Assemblies
- [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
- [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
- #endregion
- #region Generated Form Objects
- $frmPrinterQuery = New-Object System.Windows.Forms.Form
- $rdoLocation = New-Object System.Windows.Forms.RadioButton
- $lblSAPPrinter = New-Object System.Windows.Forms.Label
- $label2 = New-Object System.Windows.Forms.Label
- $lstVDetails = New-Object System.Windows.Forms.ListView
- $btnClose = New-Object System.Windows.Forms.Button
- $cmbPrintServer = New-Object System.Windows.Forms.ComboBox
- $btnCopyPath = New-Object System.Windows.Forms.Button
- $statusBar1 = New-Object System.Windows.Forms.StatusBar
- $btnSearch = New-Object System.Windows.Forms.Button
- $rdoIPAddress = New-Object System.Windows.Forms.RadioButton
- $rdoBarcode = New-Object System.Windows.Forms.RadioButton
- $label1 = New-Object System.Windows.Forms.Label
- $txtPrinterName = New-Object System.Windows.Forms.TextBox
- $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
- $PrinterHeader = New-Object System.Windows.Forms.ColumnHeader
- $ServerHeader = New-Object System.Windows.Forms.ColumnHeader
- $PortHeader = New-Object System.Windows.Forms.ColumnHeader
- $LocationHeader = New-Object System.Windows.Forms.ColumnHeader
- #endregion Generated Form Objects
- #----------------------------------------------
- #Generated Event Script Blocks
- #----------------------------------------------
- $btnCopyPath_OnClick=
- {
- #Easy copy path in format \\printserver\printer for easy install via run command.
- $printpath = "\\" + $lstVDetails.SelectedItems[0].SubItems[1].Text + "\" + $lstVDetails.SelectedItems[0].text
- $clipboardData = $printpath
- [Windows.Forms.Clipboard]::Clear();
- [Windows.Forms.Clipboard]::SetText("$clipboardData")
- }
- $lstVDetails_ItemActivate=
- {
- #Dynamic query printer status and location details and display in statusbar; Assuming your print admins are diligent with filling out all printer fields.
- $Servername = $lstVDetails.SelectedItems[0].SubItems[1].Text
- $printerdetails = Get-Printer -ComputerName ($ServerName) | Where-Object{$_.sharename -eq "$($lstVDetails.SelectedItems[0].Text)" -and $_.DeviceType -eq "Print"} | Select *
- $description = "$($printerdetails.comment) -STATUS: $($printerdetails.printerstatus)"
- #Write-Host $description
- $statusBar1.text = $description
- }
- $btnClose_OnClick=
- {
- #TODO: Place custom script here
- $frmPrinterQuery.Close()
- }
- $btnSearch_OnClick=
- {
- #TODO: Place custom script here
- #Form Validation
- $contextMenuStripService.Items.Clear()
- $lblSAPPrinter.Text = ""
- If($txtPrinterName -eq ""){$statusBar1.Text = "Searching all Printers"}
- If($rdoBarcode.Checked -eq $false -and $rdoIPAddress.Checked -eq $false -and $rdoLocation.Checked -eq $false){$statusBar1.Text = "Please select Barcode, IP Address or Location";return}
- #Clear data from form
- $lstVDetails.items.Clear()
- #inform operator of impending search
- $statusBar1.text = "Searching Print Servers.... Please Wait"
- #Check form selections - radio buttons
- If($rdoBarcode.Checked)
- {
- $filter = "name"
- }
- If($rdoIPAddress.Checked)
- {
- $filter = "PortName"
- }
- If($rdoLocation.Checked)
- {
- $filter = "location"
- }
- #Determine what server chosen in list;
- Switch($cmbPrintServer.Text)
- {
- "All Print Servers" #All printers in list;
- {
- #Advise operator of lengthy query of multiple print servers
- $statusBar1.text = "WARNING: This may take a while.."
- #Cycle through all servers in drop down list;
- $cmbPrintServer.Items | ForEach{
- #Validate server is on for query
- If(Test-Connection $_ -quiet -Count 1)
- {
- #Store Get-Printer query result based on radio button selected
- $Found += Get-Printer -ComputerName $_ -ErrorAction SilentlyContinue | Where-Object{$_.($filter) -like "*$($txtPrinterName.text)*" -and $_.DeviceType -eq "Print"} | Select *
- }
- }
- }
- Default #All other selected printers in drop down list
- {
- #Validate server is on for query
- If(Test-Connection $cmbPrintServer.Text -quiet -Count 1)
- {
- #Store Get-Printer query result based on radio button selected
- $Found = Get-Printer -ComputerName $cmbPrintServer.Text -ErrorAction SilentlyContinue | Where-Object{$_.($filter) -like "*$($txtPrinterName.text)*" -and $_.DeviceType -eq "Print"} | Select *
- }
- }
- }
- If($Found.count -gt 1) #Query multiple records found;
- {
- ForEach($record in $found)
- {
- #Add list items based on multiple selection
- AddRecord -Column1 $record.ShareName -Column2 $record.ComputerName -Column3 $record.PortName -Column4 $record.Location -status $record.PrinterStatus
- }
- }Else{
- If($Found)
- {
- #Validate variable is not null and add to list.
- AddRecord -Column1 $Found.ShareName -Column2 $Found.ComputerName -Column3 $Found.PortName -Column4 $Found.Location -status $record.PrinterStatus
- }
- }
- #Change the select mode for each row
- $lstVDetails.FullRowSelect = $True
- #Invoke right click menu only if there are items in the list.
- If($lstVDetails.Items.Count -gt 0)
- {
- #Construct contextmenu for selection
- $contextMenuStripService.Items.Add('Printer Configuration Site')
- }
- #Inform operator that the search is now complete and return result count.
- $statusBar1.text = "Search Completed - $($lstVDetails.Items.Count) printers found.."
- #Set variables to null
- $Found = $null
- }
- $handler_rightclickmenu=[System.Windows.Forms.ToolStripItemClickedEventHandler]{
- #Handy right click option to connect to a printer's web portal if available;
- Switch($_.clickeditem.text){
- "Printer Configuration Site"{Start-Process -FilePath "C:\Program Files\Internet Explorer\iexplore.exe" -ArgumentList "http://$($lstVDetails.SelectedItems[0].SubItems[2].Text)"}
- }
- }
- $OnLoadForm_StateCorrection=
- {#Correct the initial state of the form to prevent the .Net maximized form issue
- $frmPrinterQuery.WindowState = $InitialFormWindowState
- }
- $OnLoadForm_StoreVars={
- #printservers.csv contains a list of print servers on your network
- If(test-path "c:\temp\printservers.csv")
- {
- $cmbPrintServer.BeginUpdate()
- $PrinterList = Import-Csv "c:\temp\printservers.csv"
- #Assume csv file contains column labelled servers
- $Global:PrintServers = $PrinterList.Servers | Sort-Object | Select -Unique
- ForEach($Server in $PrintServers){$cmbPrintServer.Items.Add($Server)}
- $cmbPrintServer.Items.Add("----------")
- $cmbPrintServer.Items.Add("All Print Servers")
- $cmbPrintServer.EndUpdate()
- }else{
- write-host "Printservers.csv doesn't exist; Please ensure there is a csv file in the following location with a column labelled 'Servers'"
- write-host -ForegroundColor Yellow "File Path - c:\temp\printservers.csv"
- $frmPrinterQuery.Close()
- #break
- }
- }
- #----------------------------------------------
- #region Generated Form Code
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 415
- $System_Drawing_Size.Width = 614
- $frmPrinterQuery.ClientSize = $System_Drawing_Size
- $frmPrinterQuery.DataBindings.DefaultDataSourceUpdateMode = 0
- $frmPrinterQuery.FormBorderStyle = 5
- $frmPrinterQuery.Name = "frmPrinterQuery"
- $frmPrinterQuery.StartPosition = 1
- $frmPrinterQuery.Text = "Find Printer Query (Windows 10 ONLY) [PowerShell cmdlet: Get-Printer]"
- $rdoLocation.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 280
- $System_Drawing_Point.Y = 43
- $rdoLocation.Location = $System_Drawing_Point
- $rdoLocation.Name = "rdoLocation"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 24
- $System_Drawing_Size.Width = 104
- $rdoLocation.Size = $System_Drawing_Size
- $rdoLocation.TabIndex = 12
- $rdoLocation.TabStop = $True
- $rdoLocation.Text = "Location"
- $rdoLocation.UseVisualStyleBackColor = $True
- $frmPrinterQuery.Controls.Add($rdoLocation)
- $btnClose.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 516
- $System_Drawing_Point.Y = 360
- $btnClose.Location = $System_Drawing_Point
- $btnClose.Name = "btnClose"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 27
- $System_Drawing_Size.Width = 86
- $btnClose.Size = $System_Drawing_Size
- $btnClose.TabIndex = 10
- $btnClose.Text = "Close"
- $btnClose.UseVisualStyleBackColor = $True
- $btnClose.add_Click($btnClose_OnClick)
- $frmPrinterQuery.Controls.Add($btnClose)
- $lblSAPPrinter.DataBindings.DefaultDataSourceUpdateMode = 0
- $lblSAPPrinter.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,0)
- $lblSAPPrinter.ForeColor = [System.Drawing.Color]::FromArgb(255,0,128,0)
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 133
- $System_Drawing_Point.Y = 368
- $lblSAPPrinter.Location = $System_Drawing_Point
- $lblSAPPrinter.Name = "lblSAPPrinter"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 18
- $System_Drawing_Size.Width = 370
- $lblSAPPrinter.Size = $System_Drawing_Size
- $lblSAPPrinter.TabIndex = 12
- $lblSAPPrinter.Visible = $False
- $frmPrinterQuery.Controls.Add($lblSAPPrinter)
- $label2.DataBindings.DefaultDataSourceUpdateMode = 0
- $label2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,0,3,0)
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 52
- $System_Drawing_Point.Y = 75
- $label2.Location = $System_Drawing_Point
- $label2.Name = "label2"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 21
- $System_Drawing_Size.Width = 62
- $label2.Size = $System_Drawing_Size
- $label2.TabIndex = 11
- $label2.Text = "Server:"
- $label2.TextAlign = 64
- $frmPrinterQuery.Controls.Add($label2)
- $lstVDetails.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,192)
- $lstVDetails.Font = New-Object System.Drawing.Font("Arial",10,0,3,0)
- $lstVDetails.ForeColor = [System.Drawing.Color]::FromArgb(255,244,164,96)
- $lstVDetails.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 16
- $System_Drawing_Point.Y = 111
- $lstVDetails.Location = $System_Drawing_Point
- $lstVDetails.Name = "lstVDetails"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 243
- $System_Drawing_Size.Width = 586
- $lstVDetails.Size = $System_Drawing_Size
- $lstVDetails.TabIndex = 9
- $lstVDetails.UseCompatibleStateImageBehavior = $False
- $lstVDetails.View = 1
- $lstVDetails.Columns.Add($PrinterHeader)
- $PrinterHeader.Text = "Printer"
- $PrinterHeader.Width = -2
- $lstVDetails.Columns.Add($ServerHeader)
- $ServerHeader.Text = "Server"
- $ServerHeader.Width = -2
- $lstVDetails.Columns.Add($PortHeader)
- $PortHeader.Text = "Port"
- $PortHeader.Width = -2
- $lstVDetails.Columns.Add($LocationHeader)
- $LocationHeader.Text = "Location"
- $LocationHeader.Width = -2
- $contextMenuStripService = New-Object System.Windows.Forms.ContextMenuStrip
- $contextMenuStripService.add_ItemClicked($handler_rightclickmenu)
- $lstVDetails.add_Click($lstVDetails_ItemActivate)
- $lstVDetails.ContextMenuStrip = $contextMenuStripService
- $frmPrinterQuery.Controls.Add($lstVDetails)
- $cmbPrintServer.DataBindings.DefaultDataSourceUpdateMode = 0
- $cmbPrintServer.FormattingEnabled = $True
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 123
- $System_Drawing_Point.Y = 75
- $cmbPrintServer.Location = $System_Drawing_Point
- $cmbPrintServer.Name = "cmbPrintServer"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 21
- $System_Drawing_Size.Width = 199
- $cmbPrintServer.Size = $System_Drawing_Size
- $cmbPrintServer.TabIndex = 8
- $frmPrinterQuery.Controls.Add($cmbPrintServer)
- $btnCopyPath.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 16
- $System_Drawing_Point.Y = 363
- $btnCopyPath.Location = $System_Drawing_Point
- $btnCopyPath.Name = "btnCopyPath"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 24
- $System_Drawing_Size.Width = 86
- $btnCopyPath.Size = $System_Drawing_Size
- $btnCopyPath.TabIndex = 7
- $btnCopyPath.Text = "Copy Path"
- $btnCopyPath.UseVisualStyleBackColor = $True
- $btnCopyPath.add_Click($btnCopyPath_OnClick)
- $frmPrinterQuery.Controls.Add($btnCopyPath)
- $statusBar1.DataBindings.DefaultDataSourceUpdateMode = 0
- $statusBar1.Font = New-Object System.Drawing.Font("Arial Narrow",12,0,3,0)
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 0
- $System_Drawing_Point.Y = 293
- $statusBar1.Location = $System_Drawing_Point
- $statusBar1.Name = "statusBar1"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 22
- $System_Drawing_Size.Width = 419
- $statusBar1.Size = $System_Drawing_Size
- $statusBar1.TabIndex = 6
- $statusBar1.Text = "Search Printer, IP Address, Location or leave blank to find all printers per site.."
- $frmPrinterQuery.Controls.Add($statusBar1)
- $btnSearch.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 338
- $System_Drawing_Point.Y = 66
- $btnSearch.Location = $System_Drawing_Point
- $btnSearch.Name = "btnSearch"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 30
- $System_Drawing_Size.Width = 75
- $btnSearch.Size = $System_Drawing_Size
- $btnSearch.TabIndex = 5
- $btnSearch.Text = "Search.."
- $btnSearch.UseVisualStyleBackColor = $True
- $btnSearch.add_Click($btnSearch_OnClick)
- $frmPrinterQuery.Controls.Add($btnSearch)
- $rdoIPAddress.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 194
- $System_Drawing_Point.Y = 46
- $rdoIPAddress.Location = $System_Drawing_Point
- $rdoIPAddress.Name = "rdoIPAddress"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 22
- $System_Drawing_Size.Width = 91
- $rdoIPAddress.Size = $System_Drawing_Size
- $rdoIPAddress.TabIndex = 3
- $rdoIPAddress.TabStop = $True
- $rdoIPAddress.Text = "IP Address"
- $rdoIPAddress.UseVisualStyleBackColor = $True
- $frmPrinterQuery.Controls.Add($rdoIPAddress)
- $rdoBarcode.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 120
- $System_Drawing_Point.Y = 46
- $rdoBarcode.Location = $System_Drawing_Point
- $rdoBarcode.Name = "rdoBarcode"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 24
- $System_Drawing_Size.Width = 98
- $rdoBarcode.Size = $System_Drawing_Size
- $rdoBarcode.TabIndex = 2
- $rdoBarcode.TabStop = $True
- $rdoBarcode.Text = "Barcode"
- $rdoBarcode.UseVisualStyleBackColor = $True
- $frmPrinterQuery.Controls.Add($rdoBarcode)
- $label1.DataBindings.DefaultDataSourceUpdateMode = 0
- $label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,0,3,0)
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 12
- $System_Drawing_Point.Y = 9
- $label1.Location = $System_Drawing_Point
- $label1.Name = "label1"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 40
- $System_Drawing_Size.Width = 102
- $label1.Size = $System_Drawing_Size
- $label1.TabIndex = 1
- $label1.Text = "Printer Name:"
- $label1.TextAlign = 64
- $frmPrinterQuery.Controls.Add($label1)
- $txtPrinterName.DataBindings.DefaultDataSourceUpdateMode = 0
- $txtPrinterName.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,0,3,0)
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 120
- $System_Drawing_Point.Y = 18
- $txtPrinterName.Location = $System_Drawing_Point
- $txtPrinterName.Name = "txtPrinterName"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 22
- $System_Drawing_Size.Width = 293
- $txtPrinterName.Size = $System_Drawing_Size
- $txtPrinterName.TabIndex = 0
- $frmPrinterQuery.Controls.Add($txtPrinterName)
- #endregion Generated Form Code
- #Save the initial state of the form
- $InitialFormWindowState = $frmPrinterQuery.WindowState
- #Init the OnLoad event to correct the initial state of the form
- $frmPrinterQuery.add_Load($OnLoadForm_StateCorrection)
- $frmPrinterQuery.add_Load($OnLoadForm_StoreVars)
- #Show the Form
- $frmPrinterQuery.ShowDialog()| Out-Null
- } #End Function
- #Call the Function
- Load-PrinterQueryGUI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement